0

CASE 1:

#include <stdio.h>

int main()
{
    int arr[3] = {2,3,4};
    char *p;
    p = (char*) arr;
    printf(" %p - %d\n",&p, *p);
    p = p+1;
    printf("%p - %d\n",&p, *p);

    return 0;
}

output:

0x7ffde540b000 - 2  
0x7ffde540b000 - 0

In above code value of pointer 'p' is increased by 1 - but address doesn't changed.

Again I increased the value of p by 4 (i.e p = p+4). This time I get the following output -

0x7ffe50a2dff0 - 2  
0x7ffe50a2dff0 - 3

pointer move to the location arr[1] and prints the correct value.

but address doesn't changed.

CASE 2:

Now rewritten the above code (removed '&' from print statement)

printf(" %p - %d\n", p, *p);
p = p+1;
printf(" %p - %d\n", p, *p);

output -

0x7ffdb20b9d1c - 2  
0x7ffdb20b9d1d - 0

address changed by 1.

and similarly it works correctly when I update the code with p = p+4;
Output -

0x7ffef8735d6c - 2  
0x7ffef8735d70 - 3 

Address increased by 4.

I am not getting why address is not changed, when I am using '&' in print statement (CASE I).
What is difference between '&p' and 'p' in this case.

OS is Kubuntu and compiler GCC.

4
  • 1
    Note that %p requires a (void *). If you don't cast it to that type, the result is undefined behavior. Commented Jul 8, 2018 at 17:03
  • 1
    This might seem sarcastic, but what you really need is an understanding of pointers, addresses and the involved operators. Your code is almost tailor made to learn about all of that. Get a book, do more tutorials, it will become clear. Commented Jul 8, 2018 at 17:03
  • And asking a question which is basically asking for such a fundamental explanation is asking for a tutorial, which I am sorry to say, is off-topic. This is not about "so simple, you show know". It is about "so long an answer and so unspecific". Commented Jul 8, 2018 at 17:04
  • 1
    The &p is the address in memory where the variable p is stored (located). Adding to p does not change its address, just its value. Commented Jul 8, 2018 at 17:05

3 Answers 3

1

p is a variable of type char *

as a char * variable p holds values which represent pointers to characters.

&p is the address where this variable p is stored in memory.

When you write p = p + 1 you change the value of the variable p. But the variable p is the same variable, stored at the same memory, so naturally &p doesn't change.

Sign up to request clarification or add additional context in comments.

Comments

1

the &-operator in c gives you the adress where the value is stored. so with &p you get the address where p is stored and NOT the adress of the value p points to. whichs is why the adress in your first case did not change. case 2: you removed the & so you get the value of the adress where p points to which changes once you increase p.

i hope this is clear now :)

Comments

0

The problem is not what you are doing, but how you are printing the data.

int arr[3] = {2,3,4};
char *p = (char *) arr;

printf(" %p - %d\n", p, *p);
p = p+1;
printf("%p - %d\n", p, *p);

If you substitute the apparitions of &p by just p, then you'll get the results you expected. The problem is that & is read as "get the address of", so &p will return the address in which char * p is stored, ¡and that never changes!

int arr[3] = {2,3,4};
int *p = arr;

printf(" %p - %d\n", p, *p);
p = p+1;
printf("%p - %d\n", p, *p);

That's why your second block of code works. And just above these lines you have something which actually makes more sense, in which p is an int *.

I suggest you take a look at C-Sim (disclaimer: it was created by me), and follow the tutorials. C-Sim will draw a status diagram for code such as the one above.

Hope this helps.

1 Comment

You should (must) add (void *) casts to all %p conversions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.