What is a pointer?

What is a pointer?
A derived type which holds the address of a variable.
Simply a memory location where the variable is stored in
system.
Pointer is always a whole number, no matter whether it
holds the addr of int, char, float etc
Int a=100;int *ptr; ptr=&a;
Now ptr may be pointing to addr 5000.
In the next run of the pgm ‘a’ may be stored in addr 6000
and ptr points to 6000.
Pointer will also have an address in memory.
•
•
•
•
•
What is a variable?
Variable in a program is something with a name, the value
of which can vary.
Ex: int k.
When compiler sees this, it reserves space in memory(4
bytes) to hold an integer value and associate name k
with the memory location reserved. This memory
reserved is called the address.
Later in the program if it encounters k=2, value 2 will be
placed in that memory location.
Pointer to a variable is nothing but pointer through which
we can access this variable in memory.
Hence through pointers we can change the value of a
variable in the memory itself.
Address of a variable is obtained by prefixing ‘&’ in front
of variable.ex: &k
Address in memory
• Total memory depends on the size of RAM.
• If RAM is 64K(64*1024), then we have 65536
bytes(0 to 65535).
• whenever a variable is declared, compiler allocates
memory at appropriate place where the data can be
stored.
• For ex: int m=20; Compiler allocates 4 bytes of
memory starting from say 2000. Hence the bytes
reserved for m are 2000,2001,2002 and 2003.
• Hence a variable is associated with the starting
address of allocated memory.
• Programmers do not know the address of the variable.
Why are pointers used?
• to perform dynamic memory allocation.
• To implement linked lists, trees and some other data
structures.
How is pointer declared?
int a=10;
int *ptr;
ptr = &a;
//or int * ptr, int* ptr;
ptr
a
2000
10
3000
print(ptr)2000
Print(&a)2000
Print(*ptr)10
2000
• Hence ‘*’ before a declaration of a variable says it is
a pointer variable.(int *ptr;)
• ‘*’ is not attached to data type during declaration.
Hence int* ptr1,ptr2,ptr3. Here, only ptr1 is pointer
variable, not ptr2 and ptr3.
• ‘&’ prefixed to a variable gives its address in
memory.
• ‘*’ prefixed to a pointer variable gives the value
present in the memory location pointed to by
pointer.(*ptr)
• ‘&’ is called address operator or indirection operator.
• Address is printed using %u since address is always
an unsigned integer.
• Data type of a pointer variable indicates the type of
the variable ,the pointer is pointing to and not the type
of pointer.(because pointers are always unsigned
integers)
Ex:int *ptr1;
//ptr1 is pointer to int
char *ptr2; //ptr2 is pointer to char
float *ptr3; //ptr3 is pointer to float
Here ptr1,ptr2,ptr3 are all unsigned integers.
• Any number of pointers can point to a single variable.
• A pointer can point to different items at different
statements.
Double pointer or pointer to pointer
int i=3;
int *ptr1,**ptr2;
ptr1=&i;
ptr2=&ptr1;
ptr2
4500
4800
ptr1
4000
4500
i
3
4000
• ** indicates the variable is a pointer to pointer.
• Pointer to pointer holds the address of another pointer .
NOTE: there is no limit to the pointer to pointers.But maximum
limit that is being used is 3.
Print(i);
Print(*ptr1);
Print(**ptr2);
Print(ptr2);
Print(ptr1);
Print(*ptr2);
//3
//3
//3
//4500
//4000
//4000
Ptr2 prints the value contained in its address.i.e 4500
*ptr2 prints the value contained in the address 4500.i.e
4000
**ptr2 prints the value contained in address 4000.i.e 3
How a variable is stored in memory?
(Assume integer is 2 bytes)
int k=5;
k
5
2000
This is actually stored as
00
05
00000000
2000
00000101
2001
int *ptr;
ptr=&k;
(*ptr)5. when (*ptr) is used to get the value of
k,entire 2 bytes(2000 and 2001) is considered and
hence it gives 5.
Arithmetic operations allowed on pointers
1.Incrementing pointer
2.Decrementing
3.Adding or subtracting a number from the pointer.
4.Subtracting a pointer from a pointer(in case of array).
Increment
• pointers are incremented using ‘++’ operator.
• When incremented the pointer points to the next valid
byte.
• Increment depends on the type of data, the pointer is
pointing to.
Ex: incrementing an integer pointer increments the
pointer by 2 bytes,char pointer by 1 byte and float by
4 bytes.
Decrementing a pointer
• Pointers are decremented using ‘- -’ operator.
• After decrement pointer points to the previous valid
byte based on the type of pointer.
Ex:if ptr is an integer pointer and if it points to address
2500.After ptr - -, ptr points to 2498.
if ptr is a floatpointer and if it points to address
2500.After
ptr - -, ptr points to 2496.
Adding or subtracting a number to and from the
pointer
ptr=ptr+2; //if ptr==2000. after ptr+2, ptr==2004
ptr=ptr+4 //ptr==2012
ptr=ptr-3 //ptr==2006
(here ptr is int pointer)
Arithmetic operations not allowed on pointers
1.Addition of 2 pointers.
2.Multiplying or dividing a number to a pointer .
ptr=ptr*2; //invalid
ptr=ptr/2;//invalid
Ptr1+ptr2; //invalid
Ex1:
int a, b, c,*p1,*p2,*p3,*p4;
a=2;b=3;c=4
p1=&a;p2=&b;p3=&c;
*p4=a+*p2+*(&c)?
Ex2:
Int a,*p;
P=&a;
Printf(“enter the value of a”);
Scanf(“%d”,&a);
Scanf(“%d”,p);
What is wrong in 2nd scanf?
Pointers and functions
Arguments are generally passed to functions by
1.Sending the values of arguments.
2.Sending the address of arguments.
1st method(pass by value)
• Value of actual arguments in calling function is
copied onto corresponding formal arguments in called
function.
• Changes made are not reflected in actual arguments.
Main()
{
int a=10,b=20;
print(a);
print(b);
//10
//20
Swap(a,b);
Print(a);
Print(b);
//10
//20
}
Void swap(int c, int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
Here c==20 and d==10.but actual arguments not
affected.(i.e value not changed in address.
2nd method(pass by reference)
• Passing the address of variables.
• Changes are done in the memory. Hence gets
reflected in main as well.
Main()
{
int a=10,b=20;
print(a);
//10
print(b);
//20
Swap(&a,&b);
Print(a);
//20
Print(b);
//10
}
Void swap( int *c, int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;
}
Function returning pointers
• Just like a function can return an int, float, char etc, it
can also return a pointer.
• Returning pointer means returning address of a
variable.
Main()
{
int *fptr;
int *func();
fptr=func();
print(fptr);
print(*fptr);
}
Int *func()
{
static int a=10;
return(&a);
}
//int pointer
//function which returns int pointer
//2000
//10
Pointer compatibility
Main()
{
int a=10,*iptr, m;
char b,*cptr;
iptr=&a;
cptr=&b;
m=Sizeof(a);
//2
m=Sizeof(iptr);
//4
m=Sizeof(b);
//1
m=Sizeof(cptr);
//4
}
Pointer of any data type(int,float,char) is 4 bytes long.
• Even though pointers of different types are of same
size (4 bytes),they cannot be assigned to each other.
For ex:cptr=iptr or iptr=cptr are illegal.
• This is because these pointers point to different data
types.
Void pointer
• It is a pointer type to which any pointer can be
assigned and which can be assigned to any pointer.
void *pvoid;
int *ptr1;
char *ptr2;
pvoid =ptr1;
ptr2=pvoid;
Lvalue and Rvalue
In c,an expression is either an l-value or r-value.
L-value:
• This expression is used when the object is receiving a
value.
• Usually used in the left side of ‘=‘ operator.
ex:
a=…
b[5]=…
*ptr=…
R-value:
Expression that can be used to supply a value.
ex:…=10,….=a+2,….=a*6,…=a[5]+3,a++
A variable can act as both l-value or r-value;
b=a;
a=c;
• Operators that need only L-value as operand are:
&,++,--,=
hence
&(a+2);
//error bcoz (a+2) is r-value
&5;
//error
(a+2)++;
//error
++(a+3);
//error
Pointers should be handled carefully because it
actually accesses the memory location and improper
use may harm your program and data.