Categories
c. Pointers

Pointers

Pointers are powerful features of C and C++ programming. Before we learn pointers, let’s learn about addresses in C programming.

Address in C

If you have a variable var in your program, &var will give you its address in the memory.

We have used address numerous times while using the scanf() function.

scanf("%d", &var);

Here, the value entered by the user is stored in the address of var variable. Let’s take a working example.

#include <stdio.h>
int main()
{
  int var = 5;
  printf("var: %d\n", var);

  // Notice the use of & before var
  printf("address of var: %p", &var);  
  return 0;
}

Output

var: 5 
address of var: 2686778

Note: You will probably get a different address when you run the above code.

Leave a Reply

Your email address will not be published. Required fields are marked *