C Programming Pointers
C Programming Pointers
Pointers are the powerful feature of C and (C++) programming, which differs it from other popular
programming languages like: java and Visual Basic.
Pointers are used in C program to access the memory and manipulate the address.
Reference operator(&)
If var is a variable then, &var is the address in memory.
/* Example to demonstrate use of reference operator in C programming. */
#include <stdio.h>
int main(){
int var=5;
printf("Value: %d\n",var);
printf("Address: %d",&var); //Notice, the ampersand(&) before var.
return 0;
}
Output
Value: 5
Address: 2686778
Note: You may obtain different value of address while using this code.
In above source code, value 5 is stored in the memory location 2686778. var is just the name given to that
location.
You, have already used reference operator in C program while using scanf() function.
scanf("%d",&var);
Declaration of Pointer
Dereference operator(*) are used for defining pointer variable
data_type* pointer_variable_name;
int* p;
int c;
c=22;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%d\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%d\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}
Output
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
5. Code *pc=2; change the contents of the memory location pointed by pointer pc to change to 2. Since address of
pointer pc is same as address of c, value of c also changes to 2.