0

In C, the statement int *var_name; by convention means that *var_name is of type int, which implies that var_name is an integer pointer.

But now consider this sequence of statements.

int a = 5;
int *var_name = &a;

But here how are we assigning address of a to *var_name which is of type int?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 2
    You are not assigning the address of a to `*var_name` you are assigning the address of a to `var_name`. – William Pursell Feb 14 '22 at 12:11
  • https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list I'd suggest taking a look at this list and picking a book from the "beginner" section – Marco Bonelli Feb 14 '22 at 12:13
  • 2
    Most of the confusion about pointers in C come from using the same `*` syntax during declaration and de-referencing. It's easier to simply not mix up these two concepts at all. (Same syntax confusion exists between declaring an array of size n but de-referencing it from index 0 to n-1.) – Lundin Feb 14 '22 at 12:14
  • @Lundin yes. This is quite confusing. I see there is another way to interpret it. `int* var_name` would mean `var_name` is a pointer to an `int` and then the assignment of `var_name` with an address would make sense. – Rakesh Kumar Feb 14 '22 at 12:24
  • 1
    `int*` is the type of the variable and this literally means "pointer to int". – Lundin Feb 14 '22 at 12:34
  • You might find [this section](https://www.eskimo.com/~scs/cclass/notes/sx10a.html) of some online C notes useful, especially its last few paragraphs, beginning with "Finally, a few more notes about pointer declarations". – Steve Summit Feb 14 '22 at 12:46
  • 1
    Context is everything. The two lines of code are *not* statements, they are *declarations* (and also definitions). The `=` is not the assignment operator, it is part of the syntax for a declaration with an initializer. The `*` to the left of the `=` is not the indirection operator (and is not the multiplication operator either), it is part of the declared type of the variable. The part to the right of the `=` in the declaration is the initializer for the variable. – Ian Abbott Feb 14 '22 at 12:52

3 Answers3

3

You are not assigning the address to *var_name, but to var_name. And this variable is of type "pointer to int", as you found.

Oh, and it is an initialization, not an assignment.

the busybee
  • 10,755
  • 3
  • 13
  • 30
0

That's just the way the syntax in C works.

int *p = &a;

is completely equivalent to:

int *p;
p = &a;

It's not so difficult to understand why it would not make sense if it worked the way you thought. Consider this code:

int *p;
p = &a;
*p = 42;

Now imagine flipping the last two statements, like this:

int *p;
*p = 42; // To which address do we write 42? p is not initialized.
p = &a;

So although the meaning of int *p = &a;, which is int *p; p = &a; might be unintuitive, it's actually the only sane way to define it.

klutt
  • 30,332
  • 17
  • 55
  • 95
  • 1
    And neither works in case `a` is declared as `int a;`. – Lundin Feb 14 '22 at 12:38
  • @Lundin True. Didn't intentionally copy the name of the variable. Just thought of a as in address, like I did with p as in pointer. Fixed it now. – klutt Feb 14 '22 at 13:03
0

In this declaration:

int *var_name = &a;

there is declared the variable var_name of the pointer type int * and this variable (pointer) is initialized by the address of the variable a.

It seems you mixing the symbol * in declarations and in expressions.

In declarations it denotes a pointer. In expressions it denotes the dereference operator.

Consider the following demonstration program:

#include <stdio.h>

int main( void )
{
    int a = 5;
    int *var_name = &a;

    printf( "The address stored in var_name is %p\n", ( void * )var_name );
    printf( "The address of the variable a  is %p\n", ( void * )&a );

    printf( "The value of the object pointed to by var_name is %d\n", *var_name );
    printf( "The value stored in the variable a is %d\n", a );
}

Its output might look like

The address stored in var_name is 00EFFAC0
The address of the variable a  is 00EFFAC0
The value of the object pointed to by var_name is 5
The value stored in the variable a is 5

That is in this line:

    int *var_name = &a;

*var_name means a declarator of a pointer type.

In this call:

printf( "The value of the object pointed to by var_name is %d\n", *var_name );

*var_name means an expression with the dereference operator *.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335