1st Chapter
1st Chapter
ptr
2000 a 10
3000 2000
print(ptr)2000
Print(&a)2000
Print(*ptr)10
• 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 ptr1 i
4500 4800 4000 4500 40003
• ** 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); //3
Print(*ptr1); //3
Print(**ptr2); //3
Print(ptr2); //4500
Print(ptr1); //4000
Print(*ptr2); //4000
00000000 00000101
2000
int *ptr; 2001
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); //10
print(b); //20
Swap(a,b);
Print(a); //10
Print(b); //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 pointer
int *func(); //function which returns int pointer
fptr=func();
print(fptr); //2000
print(*fptr); //10
}
Int *func()
{
static int a=10;
return(&a);
}
Pointer compatibility
Main()
{
int a=10,*iptr, m;
char b,*cptr;
iptr=&a;
cptr=&b;
m=Sizeof(a); //2
m=Sizeof(iptr); //2
m=Sizeof(b); //1
m=Sizeof(cptr); //2
}
Pointer of any data type(int,float,char) is 2 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