Pointers
What are pointers?
Uses of pointers
Pointer syntax
Pointer operators
Null Pointer
Pointer to pointer
Pointer arithmetic's
FIGURE 9-3 Pointer Constants
Computer Science: A Structured Programming
3
Approach Using C
Note
Pointer constants, drawn from the set of addresses for
a computer, exist by themselves. We cannot change
them;
we can only use them.
Computer Science: A Structured Programming
4
Approach Using C
Note
An address expression, one of the expression types in
the unary expression category, consists of an
ampersand (&) and a variable name.
Computer Science: A Structured Programming
5
Approach Using C
FIGURE 9-4 Print Character Addresses
Computer Science: A Structured Programming
6
Approach Using C
Note
A variable’s address is the first byte occupied
by the variable.
Computer Science: A Structured Programming
7
Approach Using C
FIGURE 9-5 Integer Constants and Variables
Computer Science: A Structured Programming
8
Approach Using C
FIGURE 9-6 Pointer Variable
Computer Science: A Structured Programming
9
Approach Using C
Let’s Start with Addresses
Sample Code
#include <stdio.h>
int main()
{ • var is the variable
int var = 5;
printf("var: %d\n", var); • &var will give the address of
printf("address of var: %p", &var); the variable var
return 0;
} • A special variable that can store
address is a pointer variable.
Output
var: 5 %p %u –format specifiers
address of var: 2686778
What Are Pointers?
A Place holder to hold the address of the memory location.
- Address is also a number
Memory Address Value
0x8004 ….
0x8008 1 Variable A
0x800C …
Address of
Hence, 0x8010 0x8008
variable A
- A pointer is a variable whose value is the address of another variable.
Uses Of Pointers
They have a number of useful applications.
◦ Enables us to access a variable that is defined outside the function.
◦ Can be used to pass information back and forth between a function and its reference point.
◦ More efficient in handling data tables.
◦ Reduces the length and complexity of a program.
◦ Also increases the execution speed.
Pointer syntax
Different ways to initialize a pointer variable
int* p;
int *p; (Here p is a pointer)
int * p;
int* p1, p2; (Here p1 is a pointer, p2 is a normal variable)
p = &a; (Assigning the pointer p with address of a)
Contd…
int nRate;
nRate = 10;
int *pTonRate;
pTonRate = &nRate
Description
#Sample code Output
void main()
Value at *p = 6
Variable Initialization
{
Address stored in p =aef13dd4
int a = 6; Pointer Variable Declaration Address of a =aef13dd4
int *p; p is a pointer
* is a the value at operator
p = &a;
printf("Value at *p = %d\n",*p);
Store address of a in pointer variable p Note
printf("Address stored in p =%x\n", p); & ‘address of’ operator The value of a can be set using p -
printf("Address of a =%p\n", &a);
*p = 6 (after pointing p to &a)
}
Pointer Operators
& Memory Address Value
- “Address of operator” 0x8004 ….
- Provides the address of the variable
0x8008 1 a
* 0x800C …
- “De-referencing/indirection Operator” 0x8010 0x8008 p
Or p = &a
- “Value at Operator” *p = 1
?
- Accesses the memory location this pointer holds the
address of
Example
EX -1 • EX -2
int* pc, c; int* pc, c;
c = 5; c = 5;
pc = &c; pc = &c;
c = 1; *pc = 1;
printf("%d", c); // Output: 1 printf("%d", *pc); // Output: 1
printf("%d", *pc); // Output: 1 printf("%d", c); // Output: 1
The value of c is set to 1. The value of *pc is set to 1.
Since pc and the address of c is the Since pc and the address of c is the
same, *pc gives us 1. same, c gives us 1.
Example
#include <stdio.h> pc c
int main()
11
2
22
{
int* pc, c;
c = 22; Address of c : 2686784
printf("Address of c: %p\n", &c); Value of c : 22
printf("Value of c: %d\n\n", c);
pc = &c;
printf("Address of pointer pc: %p\n", pc); Address of pointer pc : 2686784
printf("Content of pointer pc: %d\n\n", *pc);
c = 11;
Content of pointer pc : 22
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); Address of pointer pc : 2686784
*pc = 2;
printf("Address of c: %p\n", &c); Content of pointer pc : 11
printf("Value of c: %d\n\n", c);
return 0; } Address of c : 2686784
Value of c : 2
Things to Remember
• Pointer variables must always point to a data item of the same type.
float x;
int *p;
: ➔ will result in erroneous output
p = &x;
• Assigning an absolute address to a pointer variable is prohibited.
int *count;
:
count = 1268;
• Once a pointer has been assigned the address of a variable, the value of the variable can be accessed using the indirection
operator (*).
int a, b;
int *p;
:
p = &a;
b = *p; // here b=a
NULL Pointer
•If an exact address to be assigned to a pointer is not known then assign a NULL value.
•This is done at the time of variable declaration.
•A pointer that is assigned NULL is called a null pointer.
•Example :-
int *ptr = NULL
(The value of ptr is 0 )
Pointer to Pointer
Pointer to pointer is a chain of
pointers.
It is declared using an extra ‘*’.
int **var;
int num = 123
int *pr2;
int **pr1;
pr2 = &num
pr1 = &pr2
Example
Sample Code Let 100 be the address of var pptr ptr var
Let 200 be the address of ptr
#include <stdio.h> 200 100 3
int main () {
200 100 address
int var;
*ptr
int *ptr;
int **pptr;
**pptr
var = 3 ;
ptr = &var;
Output
pptr = &ptr;
printf("Value of var = %d\n", var );
Value of var = 3
printf("Value available at *ptr = %d\n", *ptr );
Value available at *ptr =3
printf("Value available at **pptr = %d\n", **pptr); Value available at **pptr =3
return 0;
}
Pointer Arithmetic
•As pointer’s are numbers arithmetic operations can be performed.
•The four arithmetic operations are:
++, --, +, and –
•Incrementing/ Decrementing operator
• – Add/subtract an integer to a pointer to point to a different location
•What are not allowed?
• Adding two pointers.
p1 = p1 + p2 ;
• Multiply / divide a pointer in an expression.
p1 = p2 / 5 ;
p1 = p1 – p2 * 10 ;
Contd…
• We have seen that an integer value can be added to or subtracted from a pointer variable.
int *p1, *p2 ; p1 is a integer pointer
int i, j;
:
p1 = p1 + 1 ; Address
p2 = p1 + j ;
p2++ ;
p2 = p2 – (i + j) ;
p1 p1++
• In reality, it is not the integer value which is added/subtracted, but rather the scale factor times the value.
Data Type Scale Factor
char 1 Note that only integral values can be
int 4
float 4
added or subtracted from a pointer.
double 8 We can also subtract or compare two
If p1 is an integer pointer, then pointers of same type.
p1++
will increment the value of p1 by 4.
Example
Sample Code Explanation
#include <stdio.h> • Generally if an integer value ‘x’ is
int main() added to pointer ‘p’ then resultant value
{ is p+ x*(sizeof(p)).
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; • If ptr is 100, then ptr2 = 100 + 3*(4) =
float *ptr1 = &arr[0]; 112, and hence address belongs to
float *ptr2 = ptr1 + 3; arr[3].
printf("%d", ptr2-ptr1); • ptr2 – ptr1 = (ptr1+3 –ptr1) = 3
return 0; (which basically means no. of elements
} between ptr2 and ptr1)
Output
3
Question-1
What is the output ?
Sample Code Options Solution
void main()
D) None of the above
{ A) 6
Beacause *p points to a
float a=6, *p; B) Error
float value , the output
p = &a; C) Garbage value
will be 6.000000
printf(“%f”,*p); D) None of the above
}
Question-2
What is the output ?
Sample Code Options Solution
void main()
A) 6
{ A) 6
First the address of ‘a’
int a=6, *p; B) Error
is taken then the value
p = &a; C) Garbage value
present in there is
printf(“%d”,*(&a)); D) None of the above
printed
}
Question-3
What is the output ?
Sample Code Options Solution
void main()
A) 6
{ A) 6
&(*p) will point to p
int a=6,*p; B) Error
and *p will point to a
p = &a; C) Garbage value
that is 6
printf("%d",**(&p)); D) None of the above
}
Question-4
What is the output ?
Sample Code Options Solution
#include <stdio.h>
D) None of the above
void main() A) 6
Gives Segmentation
{ B) Address of ‘a’
fault , because as p is
int a=6,*p; C) Error
not initialized it gives a
printf("%d",*p); D) None of the above
segementation fault
}
Question-5
What is the output ?
Sample Code Options Solution
#include <stdio.h>
void main()
A) 0
{ A) 0
B) Error
int a = NULL,*c=NULL; The integer equivalent
C) Garbage value
c = &a; of NULL is 0
D) None of the above
printf("%d",*c);
}
Question-6
What is the output ?
Sample Code Options Solution
#include <stdio.h>
void main() A) 6 6
{ A) 66 Using the assignment
int a=6,*d, *c; B) Error operator to initiate the
d = &a; C) Garbage value pointer will not change
c = d; D) None of the above the working of the
printf("%d",*c); pointer
printf("%d",*d);
}
Question-7
What is the output ?
Sample Code Options Solution
#include <stdio.h> A) 2 2
void main() Updating the pointer to
A) 22
{ a new location will
B) Error
int a=2, b=4,*c,*d; change it completely to
C) Garbage value
c = &a; a new memory location.
D) None of the above
d = &b; So both ‘c’ and ‘d’
d = c; points to ‘a’ .
printf("%d",*c);
printf("%d",*d);
}
Question-8
What is the output ?
Sample Code Solution
int main()
{
int *ptr;
int x;
x=0
ptr = &x; *ptr = 0
*ptr = 0; x=5
printf(" x = %dn", x); *ptr = 5
printf(" *ptr = %dn", *ptr);
*ptr += 5; x=6
printf(" x = %dn", x); *ptr = 6
printf(" *ptr = %dn", *ptr);
(*ptr)++;
printf(" x = %dn", x);
printf(" *ptr = %dn", *ptr);
return 0;
}