Monday, 17 May 2021

Introduction to pointer in C

Definition: A pointer is a variable that stores the address of another variable.

Declaration: In C, every variable must be declared for its type. Since pointer is variable contains address that belong to a separate data type. They must be declared as pointer before we use them.

The general form to declare a pointer is

Syntax: Type *variable-name;

Example: int *p;

Initialization: The process  of assigning the address of a variable to a pointer variable is known as initialization.

Syntax: Variable-Name = address of variable;

Example: int a=10;

               int *p=&a;

Accessing a variable through its pointer:

Once a pointer has been assigned the address of a variable, to access the value of a variable, we use indirection operator(*) in front of pointer variable.

For Example consider the following statements

int a=10; int *p=&a;

Now the following statements shows how to access variable through its pointers.

  1. printf("%d",a);
  2. printf("%d",*p);
  3. printf("%d",&a);
  4. printf("%d",p);
  5. printf("%d",&p);

The first statement prints the value stored in 'a' as 10

The second statement also prints the value of 'a' through it pointer.

The third line prints the address of variable 'a'

The fourth line prints the content of pointer variable i.e what pointer stored as "200"

The fifth line prints the address of pointer variable as"400"

 For better understanding of the printf statements out put observe the above diagram.



Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial


C Programming


Java Tutorial


Data Structures


MS Office


Database Management