Examples of multilevel pointers in c:
(1) What will be output if you will execute following code?
#include<stdio.h>
#include<stdio.h>
int main(){
int s=2,*r=&s,**q=&r,***p=&q;
printf("%d",p[0][0][0]);
return 0;
return 0;
}
Output: 2
Explanation:
As we know p[i] =*(p+i)
So,
P[0][0][0]=*(p[0][0]+0)=**p[0]=***p
Another rule is: *&i=i
So,
***p=*** (&q) =**q=** (&r) =*r=*(&s) =s=2
(2) What will be output if you will execute following code?
#include<stdio.h>
#include<stdio.h>
#define int int*
int main(){
int *p,q;
p=(int *)5;
q=10;
printf("%d",q+p);
return 0;
return 0;
}
Output: 25
Explanation: If you will see intermediate file you will find following code:
#include<stdio.h>
#include<stdio.h>
void main(){
int **p,q;
p=(int **)5;
q=10;
printf("%d",q+p);
return 0;
return 0;
}
Explanations:
Explanations:
Here q pointer and p is a number.
In c
Address + number = Address
So,
New address = old address + number * Size of data type to which pointer is pointing.
= 5 + 10 * sizeof (*int)
= 5+10*2 = 25.
Note. We are assuming default pointer is near. Actually it depends upon memory model.
Pointer to function
Pointer to array of function
Pointer to array of string
Pointer to structure
pointer to union
Multi level pointer
Pointer to array of pointer to string
Pointer to three dimentional array
Pointer to two dimensional array
Sorting of array using pointer
Pointer to array of array
Pointer to array of union
Pointer to array of structure
Pointer to array of character
Pointer to array of integer
C tutorial
3 comments:
how many *'s can i put before pointer variable
Mr.siva there is no limitation to put the *'s in pointers.because every * mark to do one speacial task.
very nice theory with explanaition .please explain array theory with pictorial representation.thanks!
Post a Comment