askque
askque

Reputation: 319

usage pointer arithmetic confusion

I'm little bit confused about usage pointer arithmetic. I ask confusing question in the code as comments. I think when it is increased, the other also must be increased. Can someone explain?

#include <stdio.h>

int main()
{
    const char *str = "abcde";

    const char *temp = str; // str is pointer to address of first element of temp isn't it?

    printf("%d\n", temp - str); // zero okey

    printf("temp   str\n");

    printf("%d   %d\n", temp, str); // shows same adresses

    str++;  // hard to understand a point is here

    printf("%d   %d\n", temp, str); // why weren't also temp increased?

    temp++;

    printf("%d   %d\n", temp, str); // why weren't also str increased?

    temp++;

    printf("%d   %d\n", temp, str); // why weren't also str increased?

    return 0;
}

Upvotes: 0

Views: 94

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

This declaration

const char *str = "abcde";

means the following. There is allocated memory for variable str and it is initialized by the address of the first character of string literal "abcde"

In this declaration

const char *temp = str;

there is allocated memory for another pointer with name temp and the pointer gets a copy of the value stored in variable str.

So now you have two cells of memory each of them contains the address of the first character of the string literal.

This statement

str++;  // hard to understand a point is here

means that the value stored in str was increased. The previous value was the address of the first character of the string literal. After increasing the value now it is equal to the address of the second character of the string literal.

You can check this by executing statements with a call to function printf before this statement and after it

printf( "%s\n", str );

str++;  // hard to understand a point is here
printf( "%s\n", str );

The value stored in variable temp was not changed.

After this statement

temp++;

the both variables have equal values that are pointers to the second character of the string literal. The values stored in different cells of memory named str and temp.

After the second statement

temp++;

variable temp will have the value that equal to the address of the third character of the string literal. The value stored in variable str was not changed.

As you should know string literals include a terminating zero. You can output all characters of a string literal until the terminating zero will be encountered.

Consider the following code snippet

const char *str = "abcde";
const char *temp = str;

while ( *temp != '\0' ) printf( "%s\n", temp++ );

After the loop variable temp will have value that points to the terminating zero of the string literal. At the same time value stored in str will not be changed and will point to the first character of the string literal as before.

Upvotes: 0

Lucas Sirignano
Lucas Sirignano

Reputation: 27

const char *temp = str; means temp pointer takes value of str pointer. They are not linked. you can change str without changing temp because it's like *temp = &(str[0]); *temp value is the address of 'a' from string "abcde" if you str++ *temp value is the address of 'a' from string "abcde" and *str is the 'b' i don't know what more i wan do for you.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

In your code, temp and str are themselves two different variable, however, the data they point to, are same.

If you modify the content pointed by either of them, you can see the change reflected through other.

Also, FWIW, for printing address, change

 printf("%d   %d\n", temp, str);

to

 printf("%p   %p\n", (void *) temp, (void *)str);

otherwise, by violating the requirement of appropriate type of parameters to the format specifier, you'll invoke undefined behavior.

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

temp won't be increased by increasing str just as b won't be increased by increasing a in int a=0, b=0;. They are independent variables.

str is pointer to address of first element of "abcde" and it won't have any relationships with temp because it is defined before temp is defined.

By the way, do not try to print pointers with %d. This is undefined behavior because the types don't match.

#include <stdio.h>

int main(void)
{
    const char *str = "abcde";

    const char *temp = str; // str is pointer to address of first element of temp isn't it?

    printf("%ld\n", (long)(temp - str)); // zero okey

    printf("temp   str\n");

    printf("%p   %p\n", (void*)temp, (void*)str); // shows same adresses

    str++;  // hard to understand a point is here

    printf("%p   %p\n", (void*)temp, (void*)str); // why weren't also temp increased?

    temp++;

    printf("%p   %p\n", (void*)temp, (void*)str); // why weren't also str increased?

    temp++;

    printf("%p   %p\n", (void*)temp, (void*)str); // why weren't also str increased?

    return 0;
}

Upvotes: 0

haccks
haccks

Reputation: 106112

temp and str both are different pointer variables. Modifying any of them will not cause the modification of other but modifying the data they point to will have effect.

You should keep in mind that in your case you can modify str and temp but can't modify the string literal they points to because string literals are not modifiable.

Also note that for pointer data type %p is used as format specifier in printf to print the address they point to.

Upvotes: 1

Related Questions