Home > Mobile >  Why I can't printf value of pointer in C?
Why I can't printf value of pointer in C?

Time:11-24

I have a simple questions. I am new in pointers in C and I don't understand why this is working and I can change value of the pointer

int main()
{
    int x = 7;
    int *aptr = &x;
    printf("%d",*aptr);
    *aptr = 21;
    printf("%d",*aptr);
}

But this won't print any number

int main()
{
    int x = 7;
    int *aptr = 21;
    printf("%d",*aptr);
}

Thanks for help!

CodePudding user response:

int *aptr = 21; does not store 21 in *aptr. When = is used in a declaration, it sets the initial value for the thing being declared (which is aptr), not for the “expression picture” used for the declaration (*aptr).

In C declarations, we use pictures of a sort to describe the type. Generally, in a declaration like int declarator, declarator gives a picture of some expression we will use as an int. For example, int *foo says *foo will be an int, so it declares foo to be a pointer to an int. Another example is that int foo[3] says foo[i] will be an int, so it declares foo to be an array of int. Note that these are declaring foo, not *foo or foo[i], so, when an initial value is given, it is for initializing foo, not *foo or foo[i].

21 is not a proper value for a pointer, so the compiler complains. (Integers can be converted to pointers by using casts, but this is a special use case that requires ensuring the integers represent addresses made available in the C implementation.)

CodePudding user response:

While others provided correct answers to your question, the best way to have obtained that answer was to let the compiler tell you what the problem is. When you compile your program using, say, gcc - the default compiler on most Linux machines - you get:

$ gcc -o prog prog.c
prog.c: In function ‘main’:
prog.c:6:17: warning: initialization of ‘int *’ from ‘int’ makes pointer 
from integer without a cast [-Wint-conversion]
    6 |     int *aptr = 21;
      |                 ^~

... and this is basically what @EricPostpischil has told you :-)

Microsoft's Visual C will also say something similar (godbolt.org).

So, reading the warnings your compiler generates is very important!

Also, you should consider compiling with additional compiler warning flags enabled. Read about that here:

Why should I always enable compiler warnings?

CodePudding user response:

Pointer store address of variable not value. Here , you are declaring int *aptr as an integer pointer which means that it will store the address of any integer variable not an integer.

int x = 7;  // x is an integer variable.

int *aptr = 21;  // aptr is an integer pointer.

you should write :

 int *aptr = &x;
                              1X 
      ____                     ____
aptr | 1X |  ---------->    x | 7  |

aptr is pointing to 1X which is address of integer variable x.

CodePudding user response:

So you are currently setting the address stored in aptr to 21.

This would be equivalent to setting :

int a = 7;
int *aptr = 0xFFFFFFFF //this could be a memory address

You have simply assigned what aptr points to as the memory address 7, but you don't know what is being stored at this address (or if it's in scope) so this is why you are getting an error.

To learn more about pointers try taking a look at this article : https://www.tutorialspoint.com/cprogramming/c_pointers.htm

  • Related