C Pointer

   

A. Basic Concept of Pointer :

Pointer Concept in C Programming Language
Consider above Diagram :

  1. i is the name given for Particular memory location.
  2. Consider it’s Corresponding address be 65624 and the Value stored in variable ‘i’ is 5
  3. The address of the variable ‘i’ is stored in another integer variable whose name is ‘j’ and which is having corresponding address 65522

thus we can say that -

j = &i;

i.e

j = Address of i

Here j is not ordinary variable , It is special variable and called pointer variable as it stores the address of the another ordinary variable. We can summarize it like -

Variable Name Variable Value Variable Address
i 5 65524
j 65524 65522

B. C Pointer Basic Example :

#include <stdio.h>

int main()
{
  int *ptr, i;
  i = 11;  

  /* address of i is assigned to ptr */
  ptr = &i;   

  /* show i's value using ptr variable */      
  printf("Value of i : %d", *ptr); 

  return 0;
}

See Output and Download »

You will get value of i = 11 in the above program.

C. Pointer Declaration Tips :

1. Pointer is declared with preceding * :

int *ptr;  //Here ptr is Integer Pointer Variable
int ptr;   //Here ptr is Normal  Integer Variable

2. Whitespace while Writing Pointer :

pointer variable name and asterisk can contain whitespace because whitespace is ignored by compiler.

int *ptr;
int       * ptr;
int *           ptr;

All the above syntax are legal and valid. We can insert any number of spaces or blanks inside declaration. We can also split the declaration on multiple lines.

D. Key points for Pointer :

  1. Unline ordinary variables pointer is special type of variable which stores the address of ordinary variable.
  2. Pointer can only store the whole or integer number because address of any type of variable is considered as integer.
  3. It is good to initialize the pointer immediately after declaration
  4. & symbol is used to get address of variable
  5. * symbol is used to get value from the address given by pointer.

E. Pointer Summary :

  1. Pointer is Special Variable used to Reference and de-reference memory. (*Will be covered in upcoming chapter)
  2. When we declare integer pointer then we can only store address of integer variable into that pointer.
  3. Similarly if we declare character pointer then only the address of character variable is stored into the pointer variable.
Pointer storing the address of following DT Pointer is called as
Integer Integer Pointer
Character Character Pointer
Double Double Pointer
Float Float Pointer

You will be more familiar with pointer in the next chapter – Understanding Pointer Concept in C Programming.