Professional Documents
Culture Documents
Pointers Due To Which C Is A Unique Language
Pointers Due To Which C Is A Unique Language
65533
Auto variables on stack 65534
65535
Representation of a Variable
There are three different things associated with a
variable…..
-name of the variable
-Value of the variable
-Address of the variable
count Name
address 155 value
4000
The number of bytes required for a variable
depends on data type of variable and processor
word size.
Accessing address of a variable
Aim : want to know address of x variable…
Answer:
Apply “address of” operator ‘&’ to the
variable name.
i.e.
int x=100;
printf(“address of x %u”, &x);
printf(“value of x %d”,x);
LValue and RValue
Every variable name is associated Lvalue and
Rvalue.
When you do X = 5; then ….
5 goes to
X
using pointer.
2)Aim:- W.A.P. using pointers to find the
biggest of two given numbers.
main()
{
int i,j=25;
int *pi,*pj=&j;
*pj=j+5;
i=*pj+5;
pi=pj;
*pi=i+j;
}
Find the values of:
1) &i 9) i
2) &j 10) pi
3) pj
4) *pj
5) *pi
6) (*pi+2)
7) *(pi-1)
8) (pi+2)
Answers:
1) &i :65490 (address of i)
2) &j :65492 (address of j)
3) pj :65492 (value stored in
pj,address of j)
4) *pj: 65 (value referred by pj)
5) *pi: 65 (value referred by pi)
6) (*pi+2):67 (65+2)
7) *(pi-1):35 (65492 – 1*sizeof(int))
Answers:
8) (pi+2): 65496
(65492+2*sizeof(int))
9) i: 35
Ne w Z e a l a n d \0
A u s t r a l i a \0
I n d i a \0
Then the storage requirements for the name
table are 45 bytes.
But if we declare this using pointer,
char *name[3] = {“New Zealand”,
“Australia”,“India”};
declares name to be an array of three pointers
to characters, each pointing to a string of
varying length.
This declaration allocates only 28 bytes that are
sufficient to hold all characters.
Ne w Z e a l a n d \0
A u s t r a l i a \0
I n d i a \0
So,
name[0]New Zealand
name[1]Australia
name[2]India
The following statements would print out all the three names:-
for(1=0;i<=2;i++)
printf(“%s\n”,name[i]);