Pointer in C

Pointer in C

A pointer is a variable that is used to store memory address or sometimes it contains the address of another variable where addresses are the location of memory number always contains the whole number. So, it contains the whole number. It is called a pointer because it pointing a particular location in memory by storing the address of that memory location.

Declaration and Initialization

 Data type *pointer name;

 Here when we use * before pointer name which indicates to the compiler that variable declared as a pointer.

e.g.

int *i1;

float *f2;

char *c3;

When we declare a pointer, it always contains garbage value. there are two operators are used in the pointer i.e. address operator(&) and Indirection operator or dereference operator (*).

 Address operator returns the address of the variable, it is used in a statement of c program, Dereference operator provides the values stored at a particular address of memory location.

Example:

void main()

{

int x=15;

int *s;

s=&i;

printf(“value of x=%d”,*s);

printf(“value of x=%d”,*/&i);

printf(“address of x=%d”,&i);

printf(“address of x=%d”,s);

printf(“address of s=%u”,&s);

}

 Pointer assignment

Consider we have declared a variable

int i=5;

int *p=&i;

here  i variable is declared to store value 5 at memory location and  *p declaration tell to the compiler that variable  *p variable is declared  to store the memory address of the variable i to read the value of i

NULL Pointers in C

We can also assign a constant value 0 to a pointer of any type, it means pointer not pointing the any valid memory location for this  symbolic constant NULL is used such as.

*i=NULL;

#include<stdio.h>

int main ()

{

int *i= NULL;

printf("The value of pointer  i is : %x\n", &i );

return 0;

}

 

After compilation and execution, it produces the following result:

 The value of pointer i is : 0

Array and pointer

There are some conditions we need to use pointers and arrays together. Let us consider the following

statements:

int a[6] = {25,36,56,98,45,12};

int *i = a;

 In the first statement, we initialize the array   and the second statement, array name “a”

is the starting address of the array. Now consider starting address of the array is 2000. When “a” is an array, as we all know array name is used as address so we don’t need to (&) symbol Before a. so we have just return as int *i=a;

 To access the value in a[0] by using pointers, the indirection operator * with its pointer variable i by the notation *i can be used.

 

Value of pointer will incremented using  operator ++  with respect to size of the data type

It points, if integer data type then i++ increment i by 2 bytes and new value in i  is 2002 now *i will get you 36 which is a[1].

#include<stdio.h>

main()

{

int a[6] = {25,36,56,98,45,12};

int *i = a,j ;      /* i=&a[0] = address of the first element of array */

for (j= 0; j < 6; j++)

{

printf (“\n a[%d] = %d”,j, *i); /* increment the address */

p++;

}

}

Output:

a[0] = 25

a[1] = 36

a[2] = 56

a[3] = 98

a[4] = 45

a[5] = 12

 in this tutorial we just discussed about introduction of pointer used in c , pointer can also used with function.

Previous
Next Post »