S
sophia
Dear all,
The following is the question which i saw in a C column contest in a
magazine
what will be the output of
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[] = {10,20,30,40,50};
int *p;
p = (int*)((char*)a + sizeof(int));
printf("%d",*p);
return EXIT_SUCCESS;
}
and the winning answer was :-
This question may have different answers depending on the compiler
used(16/32 bit) and the processor used
case 1:- a 32 bit compiler and a little endian machine
here sizeof int will be 4. now a points to first element in the array
i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
00001010.
now (char*)a + 4 means (char*)a + 4 * sizeof(char)
i.e (char*)a + 4 points to 4 byte positions away from the current
position i.e it will point to byte position (00010100) of 20(00010100
00000000 00000000 00000000 ) so output will be 20 itself
case 2:- a 32 bit compiler and a big endian machine
it will point to byte position (00000000) of 20(00000000 00010100
00000000 00000000) so output will be 0
case 3:- a 16 bit compiler and a little endian machine
Here size of int will be 2 now a points to the first element in the
array i.e 10 now (char *)a + 2 points to 2 byte positions away from
the current position i.e it will point to lower byte position of 20
1.e( 00010100 00000000) so output will be 20 itself
case 4:- a 16 bit compiler and a big endian machine
it will point to byte position (00000000) of 20 i.e(00000000 00010100)
so output will be 0
how correct is the above answer ?
can any improvements be made in this answer ?
The following is the question which i saw in a C column contest in a
magazine
what will be the output of
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[] = {10,20,30,40,50};
int *p;
p = (int*)((char*)a + sizeof(int));
printf("%d",*p);
return EXIT_SUCCESS;
}
and the winning answer was :-
This question may have different answers depending on the compiler
used(16/32 bit) and the processor used
case 1:- a 32 bit compiler and a little endian machine
here sizeof int will be 4. now a points to first element in the array
i.e 10 ( 00001010 00000000 00000000 00000000) and (char*)a points to
00001010.
now (char*)a + 4 means (char*)a + 4 * sizeof(char)
i.e (char*)a + 4 points to 4 byte positions away from the current
position i.e it will point to byte position (00010100) of 20(00010100
00000000 00000000 00000000 ) so output will be 20 itself
case 2:- a 32 bit compiler and a big endian machine
it will point to byte position (00000000) of 20(00000000 00010100
00000000 00000000) so output will be 0
case 3:- a 16 bit compiler and a little endian machine
Here size of int will be 2 now a points to the first element in the
array i.e 10 now (char *)a + 2 points to 2 byte positions away from
the current position i.e it will point to lower byte position of 20
1.e( 00010100 00000000) so output will be 20 itself
case 4:- a 16 bit compiler and a big endian machine
it will point to byte position (00000000) of 20 i.e(00000000 00010100)
so output will be 0
how correct is the above answer ?
can any improvements be made in this answer ?