Concept of Pointer
Concept of Pointer
Concept of Pointer
#include <stdio.h>
int main()
{
int var = 5;
printf("Value: %d\n", var);
printf("Address: %u", &var); //Notice, the ampersand(&) before var.
return 0;
}
Output
Value: 5
Address: 2686778
Pointer variables
stores just the address of another variable. It is called Pointer variable or, simply, a pointer.
Declaration of Pointer
data_type* pointer_variable_name;
int* p;
Address of c: 2686784
Value of c: 2
NULL Pointers
A pointer that is assigned NULL is called a null pointer. NULL pointer is a constant with a value of
zero
#include <stdio.h>
int main () {
int *ptr = NULL;
printf("The value of ptr is : %x\n", ptr ); // output is 0
}
important pointer concepts
C - Pointer arithmetic
Incrementing a Pointer : ptr++ is equivalent to ptr + (sizeof(pointer_data_type)).
Decrementing a Pointer : ptr--; is equivalent to ptr - (sizeof(pointer_data_type)).
#include<stdio.h>
#include<conio.h>
void main() {
int int_var = 10, *int_ptr;
char char_var = 'A', *char_ptr;
float float_val = 4.65, *float_ptr;
/* Initialize pointers */
int_ptr = &int_var;
char_ptr = &char_var;
float_ptr = &float_val; Program Output
Address of int_var = 2293300
printf("Address of int_var = %u\n", int_ptr); Address of char_var = 2293299
printf("Address of char_var = %u\n", char_ptr); Address of float_var = 2293292
printf("Address of float_var = %u\n\n", float_ptr);
After increment address in int_ptr =
2293304
/* Incrementing pointers */ After increment address in char_ptr =
int_ptr++; 2293300
char_ptr++; After increment address in float_ptr =
float_ptr++; 2293296
printf("After increment address in int_ptr = %u\n", int_ptr);
printf("After increment address in char_ptr = %u\n", char_ptr); After addition address in int_ptr =
printf("After increment address in float_ptr = %u\n\n", float_ptr);2293312
After addition address in char_ptr =
2293302
/* Adding 2 to pointers */ After addition address in float_ptr =
int_ptr = int_ptr + 2; 2293304
char_ptr = char_ptr + 2;
float_ptr = float_ptr + 2;
}
Pointer and Arrays
int arr[5]={ 1, 2, 3, 4, 5 };
arr is equal to &arr[0] // by default
We can declare a pointer of type int to point to the array arr.
int *p;
p = arr;
or p = &arr[0]; //both the statements are equivalent.
Pointer to Array
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++;
}
Array of Pointers
char *name[3]={
"Adam",
"chris",
"Deniel"
};
//Now see same array without using pointer
char name[3][20]= {
"Adam",
"chris",
"Deniel"
};
Pointers are very helpful in handling character array with rows of varying length.
In the second approach memory wastage is more, hence it is prefered to use pointer in such
cases.
Pointer to Structure
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;
}
To access members of structure with structure variable, we used the dot . operator. But when we
have a pointer of structure type, we use arrow -> to access structure members.
Pointer as Function parameter
Pointer to functions
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
#include <stdio.h>
#include <conio.h>
int main( )
{
int (*fp)(int, int);
int s;
fp = sum;
s = fp(10, 15); // (*fp)(10,15);
printf("Sum is %d",s);
getch();
return 0;
}
Output : 25
// A simple C program to show function pointers as parameter
#include <stdio.h>
int main()
{
wrapper(fun1);
wrapper(fun2);
return 0;
}
Output fun1, fun2
C Constant Pointer and Pointer to Constant
C Constant pointer
A pointer is said to be constant pointer when the address its pointing to cannot be changed.
A constant pointer is declared as :
<type-of-pointer> *const <name-of-pointer>
Lets take an example :
char ch, c;
char *ptr = &ch
ptr = &c
In the above example we defined two characters (ch and c) and a character pointer ptr. First,
the pointer ptr contained the address of ch and in the next line it contained the address of c. In
other words, we can say that Initially ptr pointed to ch and then it pointed to c.
But in case of a constant pointer, once a pointer holds an address, it cannot change it. This means
a constant pointer, if already pointing to an address, cannot point to a new address.
If we see the example above, then if ptr would have been a constant pointer, then the third line
would have not been valid.
include<stdio.h>
int main(void)
{
char ch = 'c';
char c = 'a';
return 0;
}
When the code above is compiled, compiler gives the following error :
$ gcc -Wall constptr.c -o constptr
constptr.c: In function main:
constptr.c:9: error: assignment of read-only variable ptr
C Pointer to Constant
const <type-of-pointer> *<name-of-pointer>;
char ch = 'c';
char *ptr = &ch
*ptr = 'a';
In the above example, we used a character pointer ptr that points to character ch. In the last
line, we change the value at address pointer by ptr. But if this would have been a pointer to a
constant, then the last line would have been invalid because a pointer to a constant cannot change
the value at the address its pointing to.
#include<stdio.h>
int main(void)
{
char ch = 'c';
const char *ptr = &ch; // A constant pointer 'ptr' pointing to 'ch'
*ptr = 'a';// WRONG!!! Cannot change the value at address pointed by 'ptr'.
return 0;
}
When the above code was compiled, compiler gave the following error :
$ gcc -Wall ptr2const.c -o ptr2const
ptr2const.c: In function main:
ptr2const.c:7: error: assignment of read-only location *ptr
C Pointer to Pointer
include <stdio.h>
#include <conio.h>
int main () {
int count = 10;
/* pointer to an integer variable */
int *ptr = &count;
/* pointer to a pointer*/
int **ptrToPtr = &ptr;
getch();
return 0;
}
Value of count variable = 10
Value of count variable retreived uisng ptr 10
Value of count variable retreived uisng ptrToPtr = 10