S
somenath
Hi All,
I was going through one tutorial about C language's pointer.
After reading the tutorial I got couple of questions regarding cast
of pointer to integer .
In the exercise there is one question as mentioned .
"What is the output of following program? Assume that stack grows
towards lower addresses. Also assume that variables are pushed on to
the stack in the order in which they are declared.
"
For all the questions, assume size of int is 4, size of short is 2 and
sizeof char is 1 byte.
#include<stdio.h>
int main(void)
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", (int)p1-(int)p2);
return 0;
}
The output of the program is
4.
The explanation of the output is as given blow.
"All the local variables are stored in stack. So, x, y, p1 and p2,
all these variables will be part of the stack. Since, stack follows
"last in first out" principle, the first local variable is pushed into
the stack. i.e x is pushed first, followed by y, followed by p1 and
p2. So, &p2 and &p1 would differ by 4 bytes. Since p1 is pushed
earlier than p2, it will have higher memory address than p2. Since the
pointers are cast to integer, by subtracting them, they just behave
like normal numbers and hence output would be 4."
My questions are
1) if the size of int is 4 is it safe to assume that sizeof pointer
to int also will be 4 ?
2) Is it safe to cast pointer to integer and use them ?
3) And if the size of int and and size of pointer to int differ the
what will happen to extra bits ?
Please help me to understand these points.
Regards,
Somenath
I was going through one tutorial about C language's pointer.
After reading the tutorial I got couple of questions regarding cast
of pointer to integer .
In the exercise there is one question as mentioned .
"What is the output of following program? Assume that stack grows
towards lower addresses. Also assume that variables are pushed on to
the stack in the order in which they are declared.
"
For all the questions, assume size of int is 4, size of short is 2 and
sizeof char is 1 byte.
#include<stdio.h>
int main(void)
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", (int)p1-(int)p2);
return 0;
}
The output of the program is
4.
The explanation of the output is as given blow.
"All the local variables are stored in stack. So, x, y, p1 and p2,
all these variables will be part of the stack. Since, stack follows
"last in first out" principle, the first local variable is pushed into
the stack. i.e x is pushed first, followed by y, followed by p1 and
p2. So, &p2 and &p1 would differ by 4 bytes. Since p1 is pushed
earlier than p2, it will have higher memory address than p2. Since the
pointers are cast to integer, by subtracting them, they just behave
like normal numbers and hence output would be 4."
My questions are
1) if the size of int is 4 is it safe to assume that sizeof pointer
to int also will be 4 ?
2) Is it safe to cast pointer to integer and use them ?
3) And if the size of int and and size of pointer to int differ the
what will happen to extra bits ?
Please help me to understand these points.
Regards,
Somenath