Pr.Pg Next Pg

Pointers tutorials

  • When a variable is declared, three fundamental attributes are associated with it: its name,its type and its address in memory.

  • For example the declaration

int n;

  • Associates the name n,the type int, and the address of same location in memory where the value of n is to be stored.

  • Suppose that address is 0x125639. Then we can visualize n like this

 

Pointers

 

  • The box represents the variable's storage location in memory. The variable's name is on the left of the box, the variable’s address is above the box, and the variable’s type is below the box.

  • If the value of the variable is known, then it is shown inside the box:

 

Pointers

  • The value of a variable is accessed by means of its name. For example, we can print the value of n with the statement

printf(“%d”,n);

  • The address of a variable is accessed by means of the address operator &. For example, we can print the address of n with the statement

printf(“%u”,&n);

  • The address operator & “operates” on the variables’s name to produce its address.

Pointer declarartion

  • Pointer variables, like all other variables, must be declared before they may be used in a program.

  • The interpretation of a pointer declaration is somewhat different, however, than the interpretation of other variable declarations.

  • When a pointer variable is declared, the variable name must be preceded by an asterisk(*).

  • This identifies the fact that the variable is a pointer. The data type that appears in the declaration refers to the object of the pointer i.e. the data item that is stored in the address represented by the pointer, rather than the pointer itself.

  • Thus a pointer declaration is written as

data_type *variable_name;


 

Example: C Program to print the memory address of an integer variable

 Sam Madam

// C Program to print the memory address of an integer variable

#include<stdio.h>

#include <conio.h>

void main()

{

int n=13;

printf("\nValue of n = %d",n);

printf("\nMemory address of n = %u",&n);

getch();

}

Output

 

Value of n = 13

Memory address of n = 8712

 

 

Example: C Program to print the memory address of a float variable

Sam Madam

//C Program to print the memory address of a float variable

#include<stdio.h>

#include <conio.h>

void main()

{

float n=13.5;

printf("\nValue of n = %f",n);

printf("\nMemory address of n = %u",&n);

getch();

}

Output

Value of n = 13.500000

Memory address of n = 9244

 

Assigning the memory address of a variable to another pointer variable

  • The process of assigning the memory address of a variable to another pointer variable is known as giving the reference to another variable.

  • A reference is an alias. It is declared by using the reference operator & appended to the reference’s type. & is an unary operator. The reference operator & returns the memory address of the variable to which it is applied.

  • This type of the variable that stores an ddress is called a pointer. If the variable has type int, then the pointer variable must have type “pointer to int”, denoted by int*.

 

 

Example: C Program to study a pointer variable pointing to an integer variable

Sam Madam

// Program to study a pointer variable pointing to an integer variable

#include<stdio.h>

#include <conio.h>

void main()

{

int *p,x=10;

printf("%d address of x %u\n",x,&x);

p=&x;

*p=100;

printf("p= %d\n x= %d",p,x);

getch();

}

Output

10 address of x 8698

p= 8698

x= 8999

 

 

Example: C Program to add two integers and print the memory addresses of the two operands as well as its result

Sam Madam

/* C Program to add two integers and print the memory addresses of the two operands as well as its result*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,s;

printf("Enter 2 nos :");

scanf("%d %d",&a,&b);

s=a+b;

printf("The sum is %d\n",s);

printf("Memory address of a is %u\n",&a);

printf("Memory address of b is %u\n",&b);

printf("Memory address of s is %u",&s);

getch();

}

Output

Enter 2 nos :2 3

The sum is 5

Memory address of a is 9172

Memory address of b is 9170

Memory address of s is 9168

Pr.Pg border                                              Next Pg