I wrote the following Code.
#include<stdio.h>
int main()
{
int x = 1 ;
int *j = &x ;
int y = 2 ;
int *t = &y ;
printf("%p
" , (void *)j);
printf("%p" , (void *)t);
}
Output is 0028FF14 0028FF10
.
The Point I want to make is that the difference between the addresses is `4'.
Whereas in this case
#include<stdio.h>
int main()
{
char x = 't' ;
char *j = &x ;
char y = 'f' ;
char *t = &y ;
printf("%p
" , (void *)j);
printf("%p" , (void *)t);
}
Output is 0028FF17 0028FF16
The difference is 1
.
Difference In First Case is 4
. Whereas in the second case it is 1
. Why is it so?
And What will I get if I printed value at all memory addresses individually?
Maybe It is really general and known, but I just started C, So the output of the program confuses me.
Update
Now Using %p
format and converted the pointer value to void*
to print the pointer value as suggested by Keith Thompson.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…