0
#include <stdio.h>

int main () {

   int  var = 20;   /* actual variable declaration */
   int  *ip;        /* pointer variable declaration */

   ip = &var;  /* store address of var in pointer variable*/

   printf("Address of var variable: %x\n", &var  );

   /* address stored in pointer variable */
   printf("Address stored in ip variable: %x\n", ip );

   /* access the value using the pointer */
   printf("Value of *ip variable: %d\n", *ip );

   return 0;
}

In this example, there is *ip and there is ip. I know that *ip is used to declare the pointer, but is *ip the same thing as ip? Why didn't ip = &var use the *?

sign9
  • 1
  • The declaration could also be `int* ip` – stark Nov 09 '20 at 12:46
  • The `*`, `[]`, and `()` operators all have different meanings in a *declaration* vs. an *expression*. In a declaration, `*` indicates that the thing being declared has pointer type. The *variable* `ip` is a stores a pointer value (an address); in this case, the result of `&var`. The *expression* `*ip` in the last `printf` statement *dereferences* the variable `ip` and evaluates to the value of whatever `ip` is pointing to. – John Bode Nov 09 '20 at 15:25

0 Answers0