Pointers in C Programming

In    programming, variable is used to hole the value & this variable stored in a memory.  Each variable has a address to identify where these variables actually stored in a memory.   

 In a C programming language, pointer is a variable which can hold the address of another variable.  

Accessing Address of Variable

In a system every memory location has its address & we can access the address using ampersand (&) operator.

This ampersand (&) operator  denotes an address in memory. 

Ex:  
int  i = 20;
char c = ‘a’;

In the above example, there are two variable i & c.

C Language Pointer explanation
Fig:Memory variable and address explanation using pointer

Integer variable “i” stores the value 10 at memory location 1024(for example) and character variable “c” stores the value ‘a’ at memory location 1026.

OUTPUT

If we want to print the address of variable ‘i and c’ then we need ampersand (&) operator  .

#includeint main() { int i=20; int c=’a’; printf(“\n Address of variable i = %d”, &i ); printf(“\n Address of variable c= %d”, &c );}

OUTPUT

Note: When you run this program, address are printed might be different.

Example: Write a program to print the address of the variable.

OUTPUT

Note: Each and every time when you run this program these variables stored in a different memory location.

Pointer Variable

pointer  variable is hold the address of another variable.

Declare a pointer variable

To declare a pointer variable, there must use  a *  (Asterisk) before variable name.

OUTPUT

Value at address

 Pointer pointing  to variable address
Fig: Pointer Pointing to a address

In the above example, there are two variable, first is normal variable ‘i’ & pointer variable ‘ptr’.

Integer variable “i” stores the value 10 at memory location 1024(for example) and pointer variable “ptr” stores the address of variable  ‘i’ at memory location 2322

Example

OUTPUT

Note: Pointer variable “ptr” prints the address of variable and “*ptr” prints the value of i. *variable name (*ptr) is also called value at address.

 Pointer in C Programming
Fig: Pointer in C Programming

OUTPUT

Note: When you run this program, address are printed might be different.