You are on page 1of 12

Pointers

In C, variables are used to hold the data values during the execution of the program. Every
variable when declared occupies certain memory location/s. For example, integer-type
variable take four bytes (depending upon the architecture of your OS) bytes memory,
character type one byte and float type four. With pointers, one can manipulate memory
addresses. It is possible to access and display the address of a memory location of a
variable using the (&) operator. Memory is arranged in a series of bytes. These series of
bytes are numbered from zero onwards. The number specified to a cell is known as the
memory address.
A pointer is a memory variable that stores a memory address. Pointer can have any
name that is legal for other variable and it is declared in the same fashion like other
variables but is always denoted by an asterisk (*) operator
Features of Pointers
Pointers save the memory space.
Execution time with pointer is faster because data is manipulated with the address,
i.e. direct access to memory location.
The memory is accessed efficiently with the pointers. The pointer assigns the
memory space and also releases it.
Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional array.
We can access elements of any type of array irrespective of its subscript range.
Pointers are used for file handling.
Pointers are used to allocate memory dynamically.
Pointer Declaration
Pointer variable can be declared as follows:
int a;
int *ptr;
a = 15;
ptr = &a;
Name of the variable

a ptr
Value at variable

15 1050
Memory Location of
the variable
1050 2050





Note: Size of pointer is always same and it depends on the architecture of the OS, whether it
is float type pointer, integer type pointer or character type pointer, it doesnt matter.
1. Write a program to display the value of a variable and its location using pointer.
void main()
{
int a = 15,*ptr;
p = &v;
printf(\nAddress of a = %d,ptr);
printf(\nValue of a = %d,*ptr);
}
OUTPUT
Address of a = 1050
Value of a = 15;

Generic Pointer or Void Pointer
When a variable is declared as being a pointer to type void it is known as a generic pointer.
Since you cannot have a variable of type void, the pointer will not point to any data and
therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it
to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different
times.


ptr
1050
2050
a
15
1050
2. Write a program to show generic pointer.
void main()
{
int i;
char c;
void *the_data;

i = 6;
c = 'a';

the_data = &i;
printf("the_data points to the integer value %d\n", *(int*) the_data);

the_data = &c;
printf("the_data now points to the character %c\n", *(char*) the_data);
}

OUTPUT

the_data points to the integer value 6
the_data now points to the character a

Note: You cannot increment or decrement the value of Generic Pointers.

Dangling Pointer or Wild Pointer
Pointers are used to store memory addresses. An improper use of pointer creates many
errors in the program. Hence, pointers should be handled cautiously. When a pointer points
to an unallocated memory location or data value whose memory is de-allocated, such a
pointer is called a wild pointer. The wild pointer generates the garbage memory location
and pendent reference. The pointer becomes wild due to following reasons:
Pointer declared but not initialized
Pointer alteration
Accessing the destroyed data
When pointer is declared and not initialized, it holds an unauthorized address. It is very
difficult to manipulate such pointers.
3. Write a program to show the wild pointer.
void main()
{
int k,*x;
printf(%u,x);
}
OUTPUT
30559
Arithmetic Operations with Pointers
Arithmetic operations on pointers are also possible. Increment, decrement, prefix and postfix
operations can be performed with the pointers. The effects of these operations are shown in
following table.
Data Type Initial Address Operation
Address after
Operation
Required
Bytes
int 4046 ++ -- 4050 4042 4
char 4053 ++ -- 4054 4052 1
float 4060 ++ -- 4064 4056 4

From the table, we can observe that, on increment of pointer variable for integers, the
address is incremented by four, i.e. 4046 is the original and on increment its value will be
4050 because integer requires four bytes (depending upon the architecture of your OS).
4. Write a program to show the effect of increment and decrement on pointer variable.
void main()
{
int x = 2, *x1;
char y = A, *y1;
float z = 2.2, *z1;
x1 = &x;
y1 = &y;
z1 = &z;
printf(The address of x = %u\n,x1);
printf(The address of y = %u\n,y1);
printf(The address of z = %u\n,z1);
x1++;
y1++;
z1++;
printf(\nAfter increment in pointers\n);
printf(Now address of x = %u\n,x1);
printf(Now address of y = %u\n,y1);
printf(Now address of z = %u\n,z1);
}
OUTPUT
The address of x = 4046
The address of y = 4053
The address of z = 4060
After increment in pointers
Now address of x = 4050
Now address of y = 4054
Now address of z = 4064
Pointers to Pointers
A pointer is known as a variable containing the address of another variable. The pointer
variables also have an address. The pointer variable containing the address of another
pointer is called the pointer to pointer. The chain can be continued to any extent. The below
given program illustrates the concepts of pointer to pointer.
5. Write a program to use different levels of pointer to pointer and display the values.
void main()
{
int a = 1;
int *b,**c,***d,****e,*****f;
b = &a;
c = &b;
d = &c;
e = &d;
f = &e;
printf(%d\n,*b);
printf(%d\n,**c);
printf(%d\n,***d);
printf(%d\n,****e);
printf(%d\n,*****f);
}
OUTPUT
1
1
1
1
1
Pointers to Structure
Like we have array of integers, array of pointer etc, we can also have array of structure
variables. And to make the use of array of structure variables efficient, we use pointers of
structure type. We can also have pointer to a single structure variable, but it is mostly used
with array of structure variables.
struct Book
{
char name[10];
int price;
};
void main()
{
struct Book a;
struct Book* ptr;
struct Book b[10];
struct Book* p;
ptr = &a;
p = b;
}
Accessing Structure Members with Pointers
To access the members of structure with structure variables we use dot (.) operator, but
when we have a pointer of structure type, we use arrow to access structure member.

Array
An array is a very popular and useful data structure used to store data elements in
successive memory location. More than one element is stored in a sequence, so it is also
called a composite data structure. An array is a linear and homogeneous data structure. An
array permits homogeneous data. It means that similar types of elements are stored
contiguously in the memory and that too under one variable name. It can be combined with a
non homogeneous structure, and a complex data structure can be created. We know that an
array of structure object can also be useful. An array can be declared of any standard or
custom data type. The array of character (String) type works somewhat differently from array
of integers and floating numbers.
Array Initialization
The array initialization can be done as:
int a[5] = {1,2,3,4,5};



a[0] refers to 1
st
element i.e. 1
a[1]refers to 2
nd
element i.e. 2
a[0] refers to 3
rd
element i.e. 3
a[0] refers to 4
th
element i.e. 4
a[0] refers to 5
th
element i.e. 5



Array Terminology
Size: Number of elements or capacity to store the elements in an array is called its size. It is
always mentioned in brackets ([ ]);
Type: Types refer to data type. It decides which type of element is stored in the array. It also
instructs the compiler to reserve memory according to data type.
Base: The address of the first element (0
th
) is a base address. The array name itself stores
address of the first element.
Index: The array name is used to refer to the array element. For example, num[x], num is
array name and x is index. The value of x begins from 0 to onwards depending on the size of
the array. The index value is always an integer.
Range: Index of an array i.e. value of x varies from lower bound to upper bound while writing
or reading elements from an array. For example in num[100] the range is 0 to 99.
6. Write a program to enter 10 integer elements in a array and find their sum.
void main()
{
int a[10],i,sum = 0;
printf(Enter the elements of the array\n);
for(i = 0;i < 10;i++){
scanf(%d,&a[i]);
sum = sum + a[i];
}
printf(\nSum of all elements of array is %d,sum);
}
OUTPUT
Enter the elements of the array
1
2
3
4
5
6
7
8
9
10

Sum of all elements of array is 55
Operations with Array
Frequently performed operations with array are:
Deletion: It involves deleting specified elements from an array.
Insertion: It involves inserting an element at a specified position in an array.
Searching: It involves searching specified elements from an array.
Merging: Merging to two arrays is an important operation with an array. The elements
of two arrays are merged into single one. One should take into account the following
points:
I. Elements of one array can be appended to end of the second array.
II. Elements of two arrays can be merged in alternate order.
III. The size of the resulting array must be more than the size of the two arrays.
Sorting: Arranging elements in a specific order either in ascending or descending
order is known as sorting.

Two Three or Multi-Dimensional Array
The C program allows array of two, three or multi dimensions. The compiler determines the
restriction on it. The syntax of multi-dimensional array is as follows:
data_type array_name[s1][s2][s3][s4][s5][s6][s7]................[sn]
where si is the size of the i
th
dimensions.
Two dimensional arrays can be initialized as:
int mat[3][3] = {
1,2,3,
4,5,6
7,8,9};

Similarly, three dimensional arrays can be initialized as:
int mat[3][3][3] = {
1,2,3,
4,5,6,
7,8,9,

1,4,7,
2,5,8,
3,6,9,

1,3,5,
2,4,6,
7,8,9};

7. Write a program to print two dimensional arrays.
void main()
{
int i,j;
int mat[3][3] = {1,2,3,4,5,6,7,8,9};
for(i = 0;i < 3; i++){
for(j = 0;j < 3;j++){
printf(%d ,mat[i][j]);
}
printf(\n);
}
OUTPUT
1 2 3
4 5 6
7 8 9


Some Questions on Pointers and Array
Q1. What is an array of pointer? How it is declared?
Q2. How one pointer points to another pointer?
Q3. Why element counting of arrays always starts from 0?
Q4. What is base address?
Q5. What is the output of the following program:
void main()
{
int num = 10,*p;
p = &num;
printf(%d,*p);
printf(%d,num);
printf(%u,&num);
printf(%u,p);
}
Q6. Array name contains base address of the array. Can we change the base address of the
array?
Q7. What is the output of the following program:
void main()
{
int i,marks[5] = {55,56,67,78,64};
for(i = 2;i <= 4;i++)
printf(\n %d,marks[i]);
}
Q8. Array elements are stored in
a) scattered memory location
b) sequential memory location
c) both a) and b)


Answers
A1. Array of pointers is a collection of addresses. Here, we store the addresses of variables
for which we have to declare the array as pointer.
Declaration of Array of Pointers
data_type *ptr_name[size];
For e.g.
int *ptr[3];
A2. A pointer can point to another pointer with the help of multi-dimensional pointer.
int a,*b,**c,***d;
b = &a;
c = &b;
d = &c;
Here, b is a pointer pointing to a variable a, c is a pointer pointing to another pointer b
and d is another pointer pointing to a pointer c.
A3. The index of array, which is of the form a[i], is converted by the compiler in the form
[a+i]. So, the index of first element is zero because [a+0] will give 'a' & the first array element
can be accessed. Due to this, we can also access the array elements as'i[a]' instead of 'a[i]',
& this will not produce an error.
A4. The address of the first element (0
th
) is a base address. The array name itself stores
address of the first element.
A5. Let us assume that the memory location of num variable in 1040. Then,
10 10 1040 1040
A6. No, we cant change the base address of the array.
A7. 67
78
64
A8. a)

You might also like