-1

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!

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 5
    In your own words, when you write `int *aptr = 21;`, what do you think that means? – Karl Knechtel Nov 23 '21 at 18:54
  • 1
    What is the error message when you try and compile/run the second code? – Scott Hunter Nov 23 '21 at 18:55
  • In the first example, you are not changing the pointer. You are changing what the pointer points to (which is the `int x` variable). Also, change your printf statements to include a terminating newline `\n` e.g. `"%d\n"` to see your output more clearly. – jarmod Nov 23 '21 at 18:55

4 Answers4

1

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.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

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?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

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

Colin McCormack
  • 137
  • 1
  • 7
  • The address of `aptr` is fixed by the compiler. The item that it is pointing to is not. – Tim Randall Nov 23 '21 at 19:02
  • Re "*So you are currently pointing the address of aptr to 21.*". No, it sets the address *stored in* `aptr`, which is to say the address to which `aptr` points. – ikegami Nov 23 '21 at 19:48
0

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.