What is Pointer?

Definition:- Pointer is a type of derived data type, used to store the memory address of defined variable or constant, so we can access the address of  that defined variable or constant. Even a Pointer can access the stored value of another Pointer also.  


Concept behind it!

Let's start with a analogy. Every storage capacity no matter what it's size will be like TB, GB or MB, every creation of huge storage unit initiated from smallest unit called bits. Which  we read as a 

1GB=1024MB, 


1MB=1024KB, 


1KB=1024Bytes, 


1Bytes=8Bits... so on. 


            Every single bits memory occupies space and every space have their own memory address inside storage device. 

That's why when there is no space left for downloading new movie we need to format or erase some data from our SD Card (just kidding, but fact). 


        In a similar way every data value that we entered into the computer, will occupy a area inside a memory, having their unique address

We can see the address value of a variable using a "&" symbol just before the variable name which I will demonstrate later through coding. 

But to access that address value, we need to define pointer variable for that specific variable or constant, so it stores' that location address and we can access it.


                At first I will show you how to locate memory address of any variable in C programming. Here we will use %p &%x format specifier to see the memory address and how they differently works, also explained below. 

Lets go with explanation.  After that I will explain how to use pointer variable.

..............................................................................

Program 1

#include<stdio.h>

int main()

{

int a=20;

printf("Defined Value of a is:%d\n",a);

printf("Memory Address of defined value of a using p format specifier is:%p\n",&a);

printf("Memory Address of defined value of a using x format specifier is:%x",&a);

return 0;

}

..............................................................................


Output:-



Address of variables in C programming
Fig-1:Address of Variable in
 C-programming 


During execution of this program, you may get random address sometime, because many software run in background

They may not hold a static position. In case if we using C programming emulator in Android devices, you see this change significantly, but not much in case of PC. 

Now a days we use higher bits system, which may result in unusual behaviour output if we uses %x as a format specifier. So in further programming we will use %p format specifier in pointer variable.

Basic pointer programming in C language

Key Point

1.Pointer variable initiate by using ‘*’ sign before the variable.

2.Declaring a pointer for a given variable, the data type of pointer must be same as declared variable data types. For example if we want to store the address of a int data type in pointer, we must assign pointer also in int data types, not in float data type. Examples are given below.

a)int p;

   int*r; //correct method


b)float value;

   Int *num;    // incorrect method

..............................................................................

Program 1.1

#include<stdio.h>

int main()

{

int *c;

int num=8;

c=&num;

// here &num mean it hold the address of variable num=8 and the address collected in variable c, declared as a pointer.

printf("Address of variable num is:%p",c);

return 0;

}


Output:


Explanation of Pointer using Example
Fig 2:- Explanation of Pointer using Example

..............................................................................

Next...