Chapter - 11
Chapter - 11
Chapter – 11
Pointers
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction
In this chapter, we will discuss
Understanding Pointers
Chain of Pointers
Pointer Expression
Array of Pointers
Pointers to Function
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction
Pointers are variables that contain memory addresses as their
values.
A variable name directly references a value.
A pointer indirectly references a value. Referencing a value
through a pointer is called indirection.
A pointer variable must be declared before it can be used.
… … 100 … 1024 …
intege pointer
r
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Introduction
. Variable: x
65,535
Value: 7
Address: 1000
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Underlying Concepts of Pointers
• Memory address within the computer
• We can not change them
• We can only use them to store data
values
Pointer
Pointers
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing the concepts of a variable
Actual location of a variable is system dependent
int x[10];
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing the concepts of a variable
#include<stdio.h>
#include<conio.h>
void main()
{
char a='A';
int b=5;
float c=10.2, d=11.544;
clrscr();
printf("a=%c &a=%u\n",a,&a);
printf("b=%d &b=%u\n",b,&b);
printf("c=%f &c=%u\n",c,&c);
printf("d=%f &d=%u\n",d,&d);
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Declaring Pointer Variable
In c, variable must be declared.
data_type *pt_name;
This tells the compiler three things about the variable pt_name.
* tells that pt_name is pointer variable
pt_name needs memory location.
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Declaring Pointer Variable- Pointer
declaration style
Pointers variables are declared similarly as normal
variables except for the addition of the unary operator.
Style-1 Style-2
int *p, x,*q; int x, *p, y;
x=10;
p=&x;
y=*p; /*Accessing x through p */
*p=20; /*Accessing 20 to x */
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
Process of assigning the address of a variable to a pointer
variable is known as initialization
int quantity;
p=&quantity; /*initialization*/
or
int *p=&quantity
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
Pointer variables always point to the corresponding type of data
For example,
float a,b;
int x,*p;
p=&a; /*Wrong*/
b=*p;
Will result in erroneous output because we are trying to assign the address of
a float variable to an integer pointer
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Initialization of Pointer Variable
We could define a pointer variable with an initial
value of NULL or 0.
int *p=NULL;
int *p=0;
With the exception of NULL and 0, no other
constant value can be assigned to a pointer
variable.
For example, following is wrong:
int *p=5360; /*absolute address */
– 12 :User-Defined
ChapterChapter – 11: Pointers’
Functions
Initialization of Pointer Variable- Pointer Flexibility
Same Pointer to different data variables in different
statement.
int x,y,z,*p; X Y Z
………….
p=&x;
………….
p=&x;
…………. P
p=&z;
………….
Different Pointers to point to the same data variable.
int x;
X Y Z
int *p1=&x;
int *p2=&x;
int *p3=&x;
……………….. P
………………..
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Accessing a variable through its
Pointer
How to access the value of a variable using
pointer?
int q,*p, n; //Declaration of q, n and pointer p
q=10; //Initialization of q
p=&q // Initialization of the address of the p
n=*p; //returns the value of a variable of which
address is stored
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x,y;
int *p;
x=10;
p=&x;
y=*p;
clrscr();
printf("Value of x is %d\n\n", x);
printf("%d is stored at address %u\n",x, &x);
printf("%d is stored at address %u\n", *&x, &x);
printf("%d is stored at address %u\n", *p, p);
printf("%u is stored at address %u\n", p, &p);
printf("%d is stored at address %u\n", y, &y);
*p=25;
printf("\nNow x=%d\n", x);
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
x y p
Declaration
65524 65522 65520 Address
X=10 10
65524 65522 65520 Address
p=&x 10 65524
65524 65522 65520 Address
y=*p 10 10 65524
Pointer to x
p2 p1 Variable
Pointer p2 contains the address of pointer variable
p1, which points to the location that contains the
desired value
Declaration of Pointer to Pointer: int **p2
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x,y;
int *p,**z;
x=10;
p=&x;
y=*p;
z=&p;
clrscr();
printf("Value of x is %d\n\n",x);
printf("x=%d is stored at address &x=%u\n",x,&x);
printf("*&x=%d is stored at address &x=%u\n",*&x,&x);
printf("*p=%d is stored at address p=%u\n",*p,p);
printf("p=%u is stored at address &p=%u\n",p,&p);
printf("y=%d is stored at address &y=%u\n",y,&y);
*p=25;
printf("\nNow x=%d\n",x);
printf("z=%u is stored at address &z=%u\n **z=%u",z,&z,**z);
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer Expressions
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer Increments and Scale Factor
p1=p1+1 or p1++
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int x=10,y=5,sum,sum1;
int *p,*q;
p=&x;
q=&y;
clrscr();
sum=*p+*q;
printf("sum=%d",sum);
printf("\np=%u q=%u",p,q);
//invalid pointer addition sum1=p+q;
*p=*p+2;
++*q;
printf("\nx=%d *p=%d *q=%d\np=%u q=%u",x,*p,*q,p,q);
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays
When an array is declared, the compiler allocates a base
address and sufficient amount storage to contain all the
array in contiguous memory location.
The base address is the location of the first element (index
0) of the array
1 2 3 4 5
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
void main()
{
int *p,sum=0,i=0;
int x[5]={1,2,3,4,5};
p=x;
clrscr();
printf("\tElement\tvalue\tAddress\n");
while(i<5)
{
printf("\tx[%d]\t%d\t%u\n",i,*p,p);
sum=sum+*p;
i++,p++;
}
printf("sum=%d\n",sum);
printf("&x[0]=%u\n",&x[0]);
printf("p=%u\n",p) ;
getch();
If we remove p++ from the
} while loop then following
output will be generated
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays
We can access every value of x using p++ to move
one element to another.
The relationship between p and x is shown as:
P=&x[0] (=65514)
P+1=&x[1] (=65516)
P+2=&x[2] (=65518)
P+3=&x[3] (=65520)
P+4=&x[4] (=65522)
Address of x[3]=65514+(3*2)=65520
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Arrays
0 P
1 P+1
2
Rows 3
4 4,0 4,3 P+4
5
6 P+6
*(P+4) *(P+4)+3
P Pointer to the First Row
p+i Pointer to the ith Row
*(p+i) Pointer to first element in the ith Row
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings
g o o d \0
str
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings
char *string1;
pritntf(“%s”, string1); or
puts(string1);
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Character strings
void main()
{
char *name="DELHI";
char *p=name;
clrscr();
printf("%s",name);
while(*p!='\0')
{
printf("\n%c is stored at %u\n",*p,p);
p++;
}
getch();
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers
Important use of pointer: To handle table of String
char name [3][25];
Following is the table named “name” which contains 3
names, each with a maximum length of 25 characters
(including null character).
Name[0]
Name[1]
Name[2]
char *name[3]={
“New Zealand”, N E W Z E A L A N D \
0 Name[0]
“Australia”, A U S T R A L I A \
Name[1]
“India”
0
I N D I A \ Name[2]
} 0
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers
The following statement would print out all three names
for(i=0;i<=2;i++)
*(name[i]+j)
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Array of Pointers
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers as Function Arguments
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
int swap(int*,int*);
void main()
{
int x,y;
x=100;
y=200;
printf("Before Exchange: x=%d y=%d\n\n",x,y);
swap(&x,&y);
printf("After Exchange: x=%d y=%d\n\n",x,y);
getch();
}
Note:
The function parameters are declared as pointers
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Function Returning Pointers
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
*large();
void main()
{
int x=10,y=5,*p;
clrscr();
p=large(&x,&y);
printf("%d",*p);
getch();
}
*large(int *a,int *b)
{
if(*a>*b)
return(a);
else
return(b);
}
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointer to Function
A function, like a variable, has a type and address location in the memory.
For example
double mul (int , int);
double (*p1)();
p1=mul;
To call mul, we may now use the pointer p1 with the list of parameters
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Compatibility and Casting
int x;
char *p;
p=(char *) &x;
Casting
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Compatibility and Casting
Void pointer:
It is generic pointer
For example
int x;
char y;
void *v;
v=&x;
v=&y;
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Structures
struct inventory
char name[30];
int number;
float price;
}product[2], ptr*;
product: Data object, array of two elements, each of the type strut inventory
ptr=product ;\\assignment
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Pointers and Structures
The pointer ptr will now point to product[0].
ptr->price
Or (*ptr).number
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Troubles with pointers
Assigning values to uninitialized pointers
int *p, m=100;
*p=m; /*Error*/
Assigning value to a pointer variable
int *p,m=100;
p=m; /*Error*/
Not dereferencing a pointer when required
int *p,x=100;
p=&x;
printf(“%d”,p); /*Error*/
Assigning the address of an uninitialized variable
int m,*p;
p=&m;
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions
Differentiate: Call by value and Call by
reference [5 marks]
Write a program which adds two integer
numbers using pointers only [3 marks]
State true/false: Pointers reduce the length
and complexity of a program [1 mark]
What do you mean by pointer? Explain
advantage and disadvantage of pointer?
Chapter – 11 : Pointers’
Previous year questions
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions
Previous year questions
Chapter – 12 :User-Defined
Chapter – 11 : Pointers’
Functions