POINTERS IN C

POINTERPointer is a variable, which stores address of another variable.

Pointer notation uses two symbols:

  • * (Asterisk) symbol. * is called ‘value at address’ operator.
  • & (Ampersand) symbol. & is called ‘address of’ operator
Pointer Address Number                = Memory Location

Note that pointer is always an integer value. It is just like a student roll number, which is always an integer value. Note that roll number cannot be represented using float value.

POINTER NOTATION

Consider the declaration, int i = 3;

This declaration tells the Turbo C++ compiler to:

  1. Reserve space in memory to hold the integer value.
  2. Associate the name i with this memory location.
  3. Store the value 3 at this location

We may represent i’s location in memory by the following memory map.

Assume that computer has selected memory location 65524 as the place to store the value 3. The location number 65524 is not a number to be relied upon, because some other time the computer may choose a different location for storing the value 3. The important point is, i’s address in memory is a number.

We can print this address number through the following program:

#include<stdio.h>

void main( ){
int i = 3 ;
printf(“Address of i = %d”, &i);
printf(“\n”);
printf(“Value of i = %d”, i);
}

The output of the above program would be:
Address of i = 65524
Value of i = 3

The other pointer operator available in C++ is ‘*’, called ‘value at address’ operator. It gives the value stored at a particular address. The ‘value at address’ operator is also called ‘indirection’ operator.

Observe carefully the output of the following program:

#include<stdio.h>

int main( )
{
int i = 3 ;
printf(“Address of i = %d”, &i);
printf(“\n”);
printf(“Value of i = %d”, i);
printf(“\n”);
printf(“Value of i = %d”, *(&i));
}

he output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3

Note that printing the value of *( &i ) is same as printing the value of i.
The expression &i gives the address of the variable i. This address can be collected in a variable, by saying, j = &i;

But remember that j is not an ordinary variable like any other integer variable. It is a variable that contains the address of other variable (i in this case). Since j is a variable the compiler must provide it space in the memory. Once again, the following memory map would illustrate the contents of i and j.

POINTER TYPES:

Integer pointer: Integer pointer means that the pointer stores address of an integer value.

Syntax of declaring int pointers:

int x = 10;        /* x is an integer variable, which stores value 10 */
int *ptr;          /* ptr is a pointer variable */
ptr = &x;          /* ptr stores address of variable x */

int *p1, *p2, *p3; //declares three integer pointers p1, p2, p3, but are not assigned anything

Example of integer pointer:

#include<stdio.h>

void main( )
{
int x = 30;                              /*declaring normal variable x and assigning 30 to it */
int *Mars;                               /* declaring pointer, whose name is Mars */
Mars = &x;                             /* now hosanna points to variable x or stores address of variable x */
printf(“%d\n”, x);                /*printing value of x */
printf(“%d\n”, Mars);         /* printing value of pointer Mars */
printf(“%d\n”, *Mars);       /* prints value at address location Mars */
printf(“%d\n”, &x);             /* same as printing value of pointer */
printf(“%d”, *(&x));             /*same as printing value of x, which is 10 */
}

Float pointer:

Float pointer means that the pointer stores address of float value.

Syntax of declaring float pointers:

float x = 25.0        /* x is an float variable, which stores value 25.0 */
float *p;             /* p is a pointer variable */
p = &x;               /* p stores address of variable x */

float *p1, *p2, *p3;  //declares three float pointers p1, p2, p3, but are not assigned anything

Example of float pointer:
#include<stdio.h>

void main( )
{
float x = 30.7;                     /*declaring normal variable x and assigning 30 to it */
float *Mars;                         /* declaring pointer, whose name is Mars */
Mars = &x;                             /* now hosanna points to variable x or stores address of variable x */
printf(“%f\n”, x);                  /*printing value of x */
printf(“%d\n”, Mars);          /* printing value of pointer Mars */
printf(“%f\n”, *Mars);          /* prints value at address location Mars */
printf(“%d\n”, &x);             /* same as printing value of pointer */
printf(“%f”, *(&x));              /*same as printing value of x, which is 10 */
}

Char pointer:

Character pointer means that the pointer stores address of character value

Syntax of declaring char pointers:

char x = ‘A’;               /* x is an char variable, which stores character A */
char *p;                       /* p is a pointer variable */
p = &x;                       /* p stores address of variable x */

char *p1, *p2, *p3;       /*declares three char pointers p1, p2, p3, but are not assigned anything*/

Example of char pointer:

#include<stdio.h>

void main( )
{
char x = ‘T’;                                          /*declaring normal variable x and assigning 30 to it */
char *Mars;                                           /* declaring pointer, whose name is Mars */
Mars = &x;                                             /* now hosanna points to variable x or stores address of variable x */
printf(“%c\n”, x);                                 /*printing value of x */
printf(“%d\n”, Mars);                           /* printing value of pointer Mars */
printf(“%c\n”, *Mars);                           /* prints value at address location Mars */
printf(“%d\n”, &x);                                 /* same as printing value of pointer */
printf(“%c”, *(&x));                                /*same as printing value of x, which is 10 */
}

Void pointer:

Void pointer is a general purpose pointer, which can store address of any type. It can store address of integer data/float data/char data.

Syntax of declaring void pointers:

int a;                             /* a is an integer variable */
float b;                           /* b is a float variable */
char c;                           /* c is a character variable */

void *ptr1, *ptr2, *ptr3;         /* declaration of 3 void type pointers */

ptr1 = &a;                       /* void pointer ptr1 stores address of variable a */
ptr2 = &b;                       /* void pointer ptr2 stores address of variable b */
ptr3 = &c;                       /* void pointer ptr3 stores address of variable c */

Example of void pointer:

#include<stdio.h>

void main( )
{
int a = 2;
float b =5.9;
char c = ‘T’;
void *ptr1, *ptr2, *ptr3;                         /* declaration of 3 void type pointers  */

ptr1 = &a;
ptr2 = &b;
ptr3 = &c;

printf(“%d\n”, a);                                /*printing value of x */
printf(“%f\n”, b);                                  /*printing value of x */
printf(“%c\n”, c);                                  /*printing value of x */

printf(“%d\n”, ptr1);                             /* printing value of pointer Mars */
printf(“%d\n”, ptr2);                           /* prints value at address location Mars */
printf(“%d\n”, ptr3);                           /* same as printing value of pointer */

}

Arrays & Pointers
#include<stdio.h>

void main( )
{
int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf(“element no. %d =”, i);
printf(“address = %d\n”, &num[i]);
}
}

The output of this program would look like this:
element no. 0 address = 65512
element no. 1 address = 65514
element no. 2 address = 65516
element no. 3 address = 65518
element no. 4 address = 65520
element no. 5 address = 65522

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »