Lesson 7 C Pointers
Lesson 7 C Pointers
Area coverage
C pointers
Pointers to arrays
Pointers to function
Pointers to string
C Pointers
1|P ag e
C programming pointers notes
Features of Pointers:
1. Pointers save memory space.
2. Execution time with pointers is faster because data are manipulated with the
address, that is, direct access to memory location.
3. Memory is accessed efficiently with the pointers. The pointer assigns and releases
the memory as well. Hence it can be said the Memory of pointers is dynamically
allocated.
4. Pointers are used with data structures. They are useful for representing two-
dimensional and multi-dimensional arrays.
5. An array, of any type can be accessed with the help of pointers, without
considering its subscript range.
6. Pointers are used for file handling.
7. Pointers are used to allocate memory dynamically.
2|P ag e
C programming pointers notes
8. In C++, a pointer declared to a base class could access the object of a derived
class. However, a pointer to a derived class cannot access the object of a base
class.
9. To create complex data structure such as linked list, where one data structure
must contain references to other data structures.
Arrays and pointers are intimately related in C++ and may be used almost
interchangeably.
An array name can be thought of as a constant pointer.
Pointers can be used to do any operation involving array subscripting.
Assume the following declarations:
o int b[ 5 ]; // create 5-element int array b
o int *bPtr; // create int pointer bPtr
//Program to find the sum of six numbers with arrays and pointers.
#include <stdio.h>
int main(){
int i,class[6],sum=0;
printf("Enter 6 numbers:\n");
for(i=0;i<6;++i){
scanf("%d",(class+i)); // (class+i) is equivalent to &class[i]
sum += *(class+i); // *(class+i) is equivalent to class[i]
}
printf("Sum=%d",sum);
return 0;
}
3|P ag e
C programming pointers notes
Pointer to an Array in C
Pointer to an array is also known as array pointer. We are using the pointer to access
the components of the array.
pointers are dynamic, which means the memory allocated can be resized later at any
point in time.
data structure that contains a group of elements. Pointers are an important for creating,
using, and destroying all types of data structures.
Library catalogues reference book through a numbering system very like a software pointer.
Once you store the address of the first element in 'p', you can access the array elements
using *p, *(p+1), *(p+2) and so on.
#include <stdio.h>
int main () {
p = balance;
4|P ag e
C programming pointers notes
return 0;
}
output
Array values using pointer
*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000
Array values using balance as address
*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000
*P+=2 X+=2
++*P ++X
(*P)++ X++
Pointer Arithmetic in C
If we increment a pointer by 1, the pointer will start pointing to the immediate next
location. This is somewhat different from the general arithmetic since the value of the
pointer will get increased by the size of the data type to which the pointer is pointing.
5|P ag e
C programming pointers notes
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p); // in our case, p will get
incremented by 4 bytes.
return 0;
Since p currently points to the location 0 after adding 1, the value will become 1, and
hence the pointer will point to the memory location 1.
Pointers to functions
Function pointers can be useful when you want to create callback mechanism, and need
to pass address of a function to another function.
#include <stdio.h>
/* function declaration */
double getAverage(int *arr, int size);
6|P ag e
C programming pointers notes
int main () {
int i, sum = 0;
double avg;
A null pointer is a pointer which points nothing. Some uses of the null pointer are:
a) To initialize a pointer variable when that pointer variable isn't assigned any valid
memory address yet.
b) To pass a null pointer to a function argument when we don't want to pass any valid
memory address
Logically, the answer is no. char *a is not a string. It is a pointer variable.. which can be used
to point the character array.
char *a = “Rohit”
char a[5] = “Rohit”
Both are two different things.
7|P ag e
C programming pointers notes
char* is how you declaring a pointer to a char variable. It’s useful when you want a string with
unknown length.
Revision Question
8|P ag e
C programming pointers notes
Remember that &i will give the address of variable i. Build and run the following
#include <stdio.h>
void main(void)
int i = 7, j = 11;
printf("&i = %p, &j = %p\n", &i, &j); // %p means access address using a pointer
Code Analysis
As you may recall, each variable has a data type, name, value and address associated
with it. In the following Figure, inside the boxes, under the label of “value”, show the
values of i and j. Likewise, on the lines under the label marked “Address”, show the
9|P ag e