Pointers In C Programming Language

aimtocode

Pointers Definition

Pointers are one of the most distinct and exciting features of C language. It provides power and flexibility to the language.

Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time.

By using pointers we can get multiple values from the function, as we know a function can return only one value but by passing arguments as pointers we can get more than one values from the pointer.

By using pointers we can design and develop complex data structures like STACK, QUEUE, LINKED LIST etc.


Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value will be stored. We can easily check this memory address, using the & symbol.

Accessing the value stored in the address using unary operator (*) which returns the value of the variable located at the address specified by its operand.

Declaration of C Pointer variable

  
  datatype *pointer_name;
  							

Data type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. void type pointer works with all data types, but is not often used.


 int *ip     // pointer to integer variable
 float *fp;  // pointer to float variable
 double *dp; // pointer to double variable
 char *cp;   // pointer to char variable

Initialization of C Pointer variable

Pointer Initialization is the process of assigning address of a variable to a pointer variable. Pointer variable can only contain address of a variable of the same data type.

Example 1:



Output:

 Value of variable a is: 10
 Value of variable a is: 10
 Address of variable a is: 0x7fff5ed98c4c
 Address of variable a is: 0x7fff5ed98c4c
 Address of pointer p is: 0x7fff5ed98c50

Example 2:



Output:

 
 80

Example 3:



Output:

 Enter two numbers : 12
 Enter two numbers : 8
 Addition is : 20
aimtocode