Module 3 - CP - CS100 - Notes - KtuQbank
Module 3 - CP - CS100 - Notes - KtuQbank
COM
Module III
Pointers: Array of pointers, structures and pointers. Example programs using
pointers and structures.
Introduction to Pointers
Pointers are variables that hold address of another variable of same data type.
Pointers are one of the most distinct and exciting features of C language. It provides power and
flexibility to the language. Although pointer may appear little confusing and complicated in the
beginning, but trust me its a powerful tool and handy to use once its mastered.
Concept of Pointer
Whenever a variable is declared, system will allocate a location to that variable in the memory, to
hold value. This location will have its own address number.
Let us assume that system has allocated memory location 80F for a variable a.
int a = 10 ;
We can access the value 10 by either using the variable name a or the address 80F. Since the
memory addresses are simply numbers they can be assigned to some other variable. The variable
that holds memory address are called pointer variables. A pointer variable is therefore nothing but
a variable that contains an address, which is a location of another variable. Value of pointer
variable will be stored in another memory location.
KTUQBANK.COM
Data type of pointer must be same as the variable, which the pointer is pointing. void type pointer
works with all data types, but isn't used oftenly.
Dereferencing of Pointer
Once a pointer has been assigned the address of a variable. To access the value of variable, pointer
is dereferenced, using the indirection operator *.
KTUQBANK.COM
int a,*p;
a = 10;
p = &a;
Assuming that the base address of arr is 1000 and each integer requires two byte, the five element
will be stored as follows
Here variable arr will give the base address, which is a constant pointer pointing to the element,
arr[0]. Therefore arr is containing the address of arr[0] i.e 1000.
arr is equal to &arr[0] // by default
Now we can access every element of array arr using p++ to move from one element to another.
NOTE : You cannot decrement a pointer once incremented. p-- won't work.
Pointer to Array
As studied above, we can use a pointer to point to an Array, and then we can use that pointer to
access the array. Lets have an example,
int i;
int a[5] = {1, 2, 3, 4, 5};
int *p = a; // same as int*p = &a[0]
for (i=0; i<5; i++)
{
printf("%d", *p);
p++;
}
In the above program, the pointer *p will print all the values stored in the array one by one. We can
KTUQBANK.COM
also use the Base address (a in above case) to act as pointer and print all the values.
is same as
a[i][j]
This creates a string and stores its address in the pointer variable str. The pointer str now points to
the first character of the string "Hello". Another important thing to note that string created using
char pointer can be assigned a value at runtime.
char *str;
str = "hello"; //thi is Legal
KTUQBANK.COM
The content of the string can be printed using printf() and puts().
printf("%s", str);
puts(str);
Notice that str is pointer to the string, it is also name of the string. Therefore we do not need to use
indirection operator *.
Array of Pointers
We can also have array of pointers. Pointers are very helpful in handling character array with rows
of varying length.
char *name[3]={
"Adam",
"chris",
"Deniel"
};
//Now see same array without using pointer
char name[3][20]= {
"Adam",
"chris",
"Deniel"
};
In the second approach memory wastage is more, hence it is prefered to use pointer in such cases.
Pointer 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.
KTUQBANK.COM
struct Book
{
char name[10];
int price;
}
int main()
{
struct Book a; //Single structure variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;
int main()
{
struct Book b;
struct Book* ptr = &b;
ptr->name = "Dan Brown"; //Accessing Structure Members
ptr->price = 500;
}
Pointer Arithmetic
Pointer arithmetic is very important to understand, if you want to have complete knowledge of
pointer. In this topic we will study how the memory addresses change when you increment a
pointer.
KTUQBANK.COM
In the above case, pointer will be of 2 bytes. And when we increment it, it will increment by 2 bytes
because int is also of 2 bytes.
float* i;
i++;
In this case, size of pointer is still 2 bytes. But now, when we increment it, it will increment by 4
bytes because float is of 4 bytes.
double* i;
i++;
Similarly, in this case, size of pointer is still 2 bytes. But now, when we increment it, it will
increment by 8 bytes because its data type is double.
long 8
float 8
double 16
Pointer as Function parameter
Pointer in function parameter list is used to hold address of argument passed during function call.
This is also known as call by reference. When a function is called by reference any change made to
the reference variable will effect the original variable.
{
int a=15;
int b=92;
int *p;
p=larger(&a, &b);
printf("%d is larger",*p);
}
Pointer to functions
It is possible to declare a pointer pointing to a function which can then be used as an argument in
another function. A pointer to a function is declared as follows,
type (*pointer-name)(parameter);
Example :
int (*sum)(); //legal declaraction of pointer to function
int *sum(); //This is not a declaraction of pointer to function
A function pointer can point to a specific function when it is assigned the name of the function.
int sum(int, int);
int (*s)(int, int);
s = sum;
s is a pointer to a function sum. Now sum can be called using function pointer s with the list of
parameter.
s (10, 20);
int main( )
{
int (*fp)(int, int);
fp = sum;
int s = fp(10, 15);
printf("Sum is %d",s);
getch();
return 0;
}
Output : 25
It appears complex but it is very simple. In this case (*foo) is a pointer to the function, whose return
type is void* and argument is of int* type.