C Programming Pointer Overview 32
C Programming Pointer Overview 32
j = &i;
i.e
j = Address of i
Here j is not ordinary variable , It is special variable and called pointer variable as it stores the
address of the another ordinary variable. We can summarize it like –
i 5 65524
j 65524 65522
1
B. C Pointer Basic Example :
#include <stdio.h>
int main()
{
int *ptr, i;
i = 11;
return 0;
}
pointer variable name and asterisk can contain whitespace because whitespace is ignored by
compiler.
int *ptr;
int * ptr;
2
int * ptr;
All the above syntax are legal and valid. We can insert any number of spaces or blanks inside
declaration. We can also split the declaration on multiple lines.
D. Key points for Pointer :
1. Unline ordinary variables pointer is special type of variable which stores the address of
ordinary variable.
2. Pointer can only store the whole or integer number because address of any type of variable
is considered as integer.
3. It is good to initialize the pointer immediately after declaration
4. & symbol is used to get address of variable
5. * symbol is used to get value from the address given by pointer.
E. Pointer Summary :
1. Pointer is Special Variable used to Reference and de-reference memory. (*Will be covered
in upcoming chapter)
2. When we declare integer pointer then we can only store address of integer variable into
that pointer.
3. Similarly if we declare character pointer then only the address of character variable is
stored into the pointer variable.
3
C pointer basic concept
Understanding pointers in c ?
In the previous chapter we have learnt about Basic Concept of Pointer. In this chapter we are
looking more into pointer.
We know that –
2 Types of Variables :
#include<stdio.h>
int main()
{
int a = 3;
int *ptr;
4
ptr = &a;
return(0);
}
Explanation of Example :
#include<stdio.h>
int main()
{
int a = 3;
int *ptr,**pptr;
ptr = &a;
pptr = &ptr;
return(0);
}
Explanation of Example
5
With reference to above program –
6
C pointer address operator
In the previous chapter we have learnt about more c programming basic pointer concept. In this
chapter we will be learning about pointer address operator in C programming.
Pointer address operator in C Programming
#include<stdio.h>
void main()
{
int n = 10;
printf("\nValue of n is : %d",n);
printf("\nValue of &n is : %u",&n);
}
Output :
Value of n is : 10
Value of &n is : 1002
Consider the above example, where we have used to print the address of the variable using
ampersand operator.
7
In order to print the variable we simply use name of variable while to print the address of the
variable we use ampersand along with %u
#include<stdio.h>
int main()
{
int i = 5;
int *ptr;
ptr = &i;
printf("\nAddress of i : %u",&i);
printf("\nValue of ptr is : %u",ptr);
return(0);
}
int i = 5;
int *ptr;
after Assigning the address of variable to pointer , i.e after the execution of this statement –
ptr = &i;
8
Invalid Use of pointer address operator
Address of literals
In C programming using address operator over literal will throw an error. We cannot use address
operator on the literal to get the address of the literal.
&75
Only variables have an address associated with them, constant entity does not have corresponding
address. Similarly we cannot use address operator over character literal –
&('a')
(a+b) will evaluate addition of values present in variables and output of (a+b) is nothing but
Literal, so we cannot use Address operator
&(a+b)
9
C pointer memory organization
In the last chapter we have learnt about Address operator in C Programming. In this chapter we
are going to learn the memory organization for pointer variable.
Memory Organization for Pointer Variable :
1. When we use variable in program then Compiler keeps some memory for that variable
depending on the data type
2. The address given to the variable is Unique with that variable name
10
3. When Program execution starts the variable name is automatically translated into the
corresponding address.
Explanation :
1. Pointer Variable is nothing but a memory address which holds another address .
2. In the above program “i” is name given for memory location for human understanding ,
but compiler is unable to recognize “i” . Compiler knows only address.
11
4. This requirement is different for different Compilers
#include<stdio.h>
int main()
{
int a = 10, *ptr;
ptr = &a;
return(0);
}
Output :
#include<stdio.h>
int main()
{
char a = 'a', *cptr;
cptr = &a;
return(0);
}
Output :
12
Size of Character Pointer : 2
#include<stdio.h>
int main()
{
float a = 3.14, *fptr;
fptr = &a;
return(0);
}
Output :
13
C size of pointer variable
In the previous chapter, we have learnt about the memory organization of the pointer variable. In
this chapter we are learning to compute or find the size of pointer variable.
Size of Pointer variable in C
Consider the following memory map before declaring the variable.In the memory comprise of
blocks of 1 byte.
Whenever we declare any variable then random block of memory is chosen and value will be
stored at that memory location.
Similarly whenever we declare any pointer variable then random block of memory will be used
as pointer variable which can hold the address of another variable.
Program
#include<stdio.h>
14
int main()
{
int *iptr = NULL;
float *fptr = NULL;
char *cptr = NULL;
return 0;
}
Output :
Explanation
15
C pointer declaration
In the previous chapter we have learnt about Pointer operators supported by C Language. In this
chapter we will be learning about declaration of Pointer Variable.
Syntax for Pointer Declaration in C :
data_type *<pointer_name>;
Explanation :
data_type
Asterisk(*)
16
pointer_name
int *p;
int * p;
int * p;
int n = 20;
int *ptr;
char ch = 'A';
char *cptr;
float *fptr;
17
C initialization of pointer
pointer = &variable;
[box]
18
[/box]
below example will clearly explain the initialization of Pointer Variable.
#include<stdio.h>
int main()
{
int a; // Step 1
int *ptr; // Step 2
a = 10; // Step 3
ptr = &a; // Step 4
return(0);
}
Note :
#include<stdio.h>
int main()
{
int a = 10;
int *ptr;
ptr = &a;
printf("\nValue of ptr : %u",ptr);
19
return(0);
}
Output :
C dereferencing pointer
20
Dereferencing Pointer in C Programming Language :
1. Asterisk(*) indirection operator is used along with pointer variable while Dereferencing
the pointer variable.
2. Asterisk Operator is also called as value at operator
3. When used with Pointer variable, it refers to variable being pointed to,this is called
as Dereferencing of Pointers
[box]
[/box]
Live Example : Dereferencing Pointer
Evaluation :
Summary :
Variable Value in it
num 50
&num 1002
ptr 1002
*ptr 50
In the next chapter we will be learning the void pointers. Void Pointer is special type of pointer
which can store the address of any variable.
22
C void pointers
In the previous chapter we have learnt about dereferencing of pointer variable. In this chapter we
will be looking special type of pointer called void pointer or general purpose pointer.
Void Pointers in C : Definition
1. Suppose we have to declare integer pointer,character pointer and float pointer then we need
to declare 3 pointer variables.
2. Instead of declaring different types of pointer variable it is feasible to declare single pointer
variable which can act as integer pointer,character pointer.
void * pointer_name;
char cnum;
int inum;
float fnum;
23
ptr = &fnum; // ptr has address of float data
Explanation :
void *ptr;
Scenario Behavior
24
C dereferencing void pointer
float *ptr;
int num;
In the above example we have declared float pointer which is of no use in the program. We cannot
use float pointer to store the integer pointer so So in order to increase the re-usability of the
Pointer it is necessary to declared pointer variable as void Pointer.
Dereferencing Void Pointer :
char cnum;
int inum;
float fnum;
*((int*)ptr)
Integer *((int*)ptr)
Charcter *((char*)ptr)
Floating *((float*)ptr)
#include<stdio.h>
int main()
{
int inum = 8;
float fnum = 67.7;
void *ptr;
ptr = &inum;
printf("\nThe value of inum = %d",*((int*)ptr));
ptr = &fnum;
printf("\nThe value of fnum = %f",*((float*)ptr));
26
return(0);
}
Output :
ptr = &inum;
printf("\nThe value of inum = %d",*((int*)ptr));
Here we are assigning the address of integer variable to void pointer so that void pointer can act
as Integer pointer.
ptr = &fnum;
printf("\nThe value of fnum = %f",*((float*)ptr));
And now we are storing the address of floating point variable to void pointer , thus void pointer
becomes floating pointer.
27
C size of void pointer
Pointer :
Pointer is the variable that stores the address of another variable .
28
4. In C , for storing integer value 2 bytes are required .
5. So void Pointer variable requires 2 bytes of memory.
int main(void)
{
int num = 0;
void *ptr;
num = 15;
ptr = #
return 0;
}
Output :
Size of Pointer : 2 Bytes
Consider below table, We have declared some of the variables and also assumed some address
for declared variables.
29
Data Initial Address after Required
Operation
Type Address Operations Bytes
Expression Result
Above table clearly shows that we can add or subtract address and integer number to get valid
address. We can even subtract two addresses but we cannot add two addresses
30
C incrementing pointer
Incrementing Pointer :
Address + 1 = Address
Address++ = Address
++Address = Address
Pictorial Representation :
31
Incrementing a pointer to an integer data will cause its value to be incremented by 2 .
This differs from compiler to compiler as memory required to store integer vary compiler
to compiler
[box]Note to Remember : Increment and Decrement Operations on pointer should be used when
we have Continues memory (in Array).[/box]
Live Example 1 : Increment Integer Pointer
#include<stdio.h>
int main(){
ptr=ptr+1;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
#include<stdio.h>
int main(){
ptr=ptr+1;
printf("New Value of ptr : %u",ptr);
32
return 0;
}
Output :
#include<stdio.h>
int main(){
float var[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];
ptr=&var;
printf("Value inside ptr : %u",ptr);
ptr=ptr+1;
printf("Value inside ptr : %u",ptr);
return 0;
}
Output :
33
Explanation :
Address of ptr[1]
= Address of ptr[0] + (Size of Data Type)*(Size of Array)
= 1000 + (4 bytes) * (5)
= 1020
Address of Var[0]…Var[4] :
34
C pointer addition
In C Programming we can add any integer number to Pointer variable. It is perfectly legal in c
programming to add integer to pointer variable.
In order to compute the final value we need to use following formulae :
35
Consider the following example –
int *ptr , n;
ptr = &n ;
ptr = ptr + 3;
#include<stdio.h>
int main(){
ptr=ptr+3;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
Explanation of Program :
this line will store 1000 in the pointer variable considering 1000 is memory location for any of
the integer variable.
Formula :
36
= 1006
37
C subtracting integer value from pointer
Suppose we have subtracted “n” from pointer of any data type having initial addess as
“init_address” then after subtraction we can write –
int *ptr , n;
ptr = &n ;
ptr = ptr - 3;
#include<stdio.h>
int main(){
ptr=ptr-3;
printf("New Value of ptr : %u",ptr);
return 0;
}
Output :
Formula :
Summary :
1. Pointer comparison is Valid only if the two pointers are Pointing to same array
2. All Relational Operators can be used for comparing pointers of same type
3. All Equality and Inequality Operators can be used with all Pointer types
4. Pointers cannot be Divided or Multiplied
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
return(0);
}
int main()
{
int *ptr1;
float *ptr2;
return(0);
}
Explanation :
[box]As we know Pointers can store Address of any data type, address of the data type is
“Integer” so we can compare address of any two pointers although they are of different data
types.[/box]
Following operations on pointers :
== Equals
!= Not Equal
40
Divide and Multiply Operations :
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
return(0);
}
Output :
41
C pointer operation rules
int main() {
int num = 10;
int *ptr;
ptr = #
return(0);
}
int *sptr,*tptr;
42
sptr = #
tptr = sptr;
int *sptr,*tptr;
sptr = 0;
tptr = NULL;
int arr[20];
int *ptr;
ptr = &arr;
ptr++;
int arr[20];
int *ptr;
ptr = &arr;
ptr = ptr + 2; //arr[2] will be accessed
6. When two Pointer points to same array then one Pointer variable can be Subtracted from
another
7. Two Pointers pointing to objects of same data type then they can be compared using the
Relational Operator
8. Value cannot be assigned to arbitrary address
int *ptr;
ptr = 100; //Illegal
43
9. Pointer Variable cannot be multiplied by Constant
int *ptr;
ptr = ptr * 6; //Illegal
One can perform different arithmetic operations on Pointer such as increment,decrement but still
we have some more arithmetic operations that cannot be performed on pointer –
44
1. Addition of two addresses.
2. Multiplying two addresses.
3. Division of two addresses.
4. Modulo operation on pointer.
5. Cannot perform bitwise AND,OR,XOR operations on pointer.
6. Cannot perform NOT operation or negation operation.
#include<stdio.h>
int main()
{
printf("%d",ptr1+ptr2);
return 0;
}
Output :
Compile Error
#include<stdio.h>
int main()
{
printf("%d",ptr1*var);
return 0;
}
Output :
Compile Error
#include<stdio.h>
int main()
{
printf("%d",ptr1*ptr2);
return 0;
}
Output :
Compile Error
4. Modulo Operation
46
int *ptr1 = (int *)1000;
int *ptr2 = (int *)2000;
int var = ptr2 % ptr1;
5. Division of pointer
#include<stdio.h>
int main()
{
int *ptr1,*ptr2;
return(0);
}
Output :
#include<stdio.h>
47
int main(){
int i=5,j=10;
int *p=&i;
int *q=&j;
printf("%d",p|q);
return 0;
}
#include<stdio.h>
int main(){
int i=5;
int *ptr=&i;
printf("%d",~ptr);
return 0;
}
Summary at a glance :
.
Precedence of Value at Operator :
Multiplication
In Pointer for De-referencing
49
3. Value at operator ‘*’ in pointer concept is also called as “De-reference” Operator Or
“Value at Operator“
C double pointer
int **ptr2ptr;
50
Consider the Following Example :
*ptr 45
**ptr2ptr 45
ptr &n
ptr2ptr &ptr
Notes :
Live Example :
#include<stdio.h>
51
int main()
{
int num = 45 , *ptr , **ptr2ptr ;
ptr = #
ptr2ptr = &ptr;
printf("%d",**ptr2ptr);
return(0);
}
Output :
45
52
C pointer to array element
int a[10];
int a[10][10];
Let
1. x is an array ,
2. i is subscript variable
x[i] = *(x+i)
= *(i+x)
= i[x]
= *x
Thus we have concluded that , first element of array is equivalent to *x .
53
2 . Thus *(x+i) will gives us value of ith element.
main()
{
int x[] = {1,2,3,4,5};
int *ptr,i ;
ptr = x
for(i=0;i<5;i++)
{
printf("nAddress : %u",&x[i]);
printf("nElement : %d",x[i]);
printf("nElement : %u",*(x+i));
printf("nElement : %d",i[x]);
printf("nElement : %d",*ptr);
}
}
Output :
Address : 7600
Element : 1
Element : 1
Element : 1
Element : 1
Address : 7602
Element : 2
Element : 2
Element : 2
Element : 2
Address : 7604
Element : 3
Element : 3
Element : 3
Element : 3
Address : 7606
Element : 4
Element : 4
Element : 4
Element : 4
Address : 7608
Element : 5
54
Element : 5
Element : 5
Element : 5
x[i] *(x + i)
i[x] *ptr
Pointer to array of string : A pointer which pointing to an array which content is string, is
known as pointer to array of strings.
55
In this example
#include<stdio.h>
int main()
{
int i;
for(i=0;i<4;i++)
printf("Address of String %d : %u\n",i+1,(*ptr)[i]);
return 0;
}
Output :
#include<stdio.h>
int main()
56
{
int i;
for(i=0;i<4;i++)
printf("String %d : %s\n",i+1,(*ptr)[i]);
return 0;
}
Output :
String 1 = C
String 2 = C++
String 3 = Java
String 4 = VBA
#include<stdio.h>
int main()
{
int i;
printf("%s",++(*ptr)[2]);
return 0;
57
}
Output:
ava
C function pointer
58
What is function Pointer ?
Function pointer : A pointer which keeps address of a function is known as function pointer.
[ Visit : Complete Reference Document for Function Pointer ]
Live Example :
#include<stdio.h>
void display();
int main()
{
void *(*ptr)();
ptr = &display;
(*ptr)();
return(0);
}
void display()
{
printf("Hello World");
}
Output :
Hello World
Explanation of C Snippet :
59
Consider Scenario using pointer , We should follow following 3 steps to use pointer to call
function –
void *(*ptr)();
Declare Pointer variable that can store address of function which does not return anything and
doesn’t take any parameter
Step 2 : Initializing Pointer
ptr = &display;
(*ptr)();
(*ptr)() = (*ptr)();
= (*&display)();
= (display)();
= display();
60
Return Type : Non
e void *(*ptr)(); ptr = &display; (*ptr)();
Parameter : None
Example 1 : Function having two Pointer Parameters and return type as Pointer
#include<stdio.h>
int main()
{
char *name;
int num = 100;
float marks = 99.12;
61
printf("Name : %s",name);
return 0;
}
//-------------------------------------
char *getName(int *ivar,float *fvar)
{
char *str="www.google.com";
str = str + (*ivar) + (int)(*fvar);
return(str);
}
62