Velocity Reviews > predict the answer

sophia
Guest
Posts: n/a

 03-07-2008
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 ?

Richard Heathfield
Guest
Posts: n/a

 03-07-2008
sophia said:

> 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 ?

Here's the code again:

#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;
}

int a[] = {10,20,30,40,50}; defines an array of int with five elements.
int *p; defines a pointer to int.
a yields &a[0], the address of the lowest-address byte in the a array,
which is guaranteed to be aligned correctly for int because it's an int
array.
(char *)a yields the same address, but with type char *.
Because arrays are guaranteed to be contiguous with no padding before or
after any array member, (char *)a + sizeof(int) yields the address of the
second int in the array, with type char *. Again, this is guaranteed to be
aligned correctly for int.
(int *) yields the same address as its operand, but with type int *.
p = assigns that address to p.
printf("%d", *p); will write "20" to the standard output device.
Guaranteed. (Well, printf can fail, and of course there's no newline, but
you know what I mean.)

It seems to me that the answer you saw is completely broken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Ian Collins
Guest
Posts: n/a

 03-07-2008
sophia wrote:
> 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
>

No, it will not. p points the the second int in the array, byte order
(or sizeof int ) has no effect.

> how correct is the above answer ?

Incorrect.

--
Ian Collins.

Guest
Posts: n/a

 03-07-2008
sophia wrote:
> 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 :-

0. Nothing, as you didn't emit a terminating \n in the printf
above (without it an implementation is not required to output
anything on the standard output).

I haven't read the rest very carefully, but they seems sound,
and also off topic in this newsgroup, as they go beyond the
Standard C, and into the particulars of implementation. I'm sure
this will be pointed out by others, too.

Guest
Posts: n/a

 03-07-2008
> I haven't read the rest very carefully, but they seems sound, and also

Obviously so (I actually didn't go past the 1. and a bit of 2.).

Richard and Ian were of course correct.

Sophia, are you sure you copied the code correctly, as there are
ways to make it behave in a fashion similar to the given
answers? This is just not one of those.

Robbie Hatley
Guest
Posts: n/a

 03-07-2008

"sophia" <(E-Mail Removed)> wrote:

> 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 ?

Not correct at all. It will always print 20, because the
pointer always points at the 20, and it's always an int
pointer.

The answer is wrong for the code. But I think your code

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[] = {10,20,30,40,50};
int b = 0;

b = (int)(*((char*)a + sizeof(int)));
printf("%d\n", b);
return EXIT_SUCCESS;
}

Now I think *THAT* will produce just the answers you gave,
because the pointer is now a char pointer, not an int
pointer, so when you dereference it with the "*" I think
it gives the byte that it points to (the lowest-RAM-address
byte of the "20"), cast back to an int. So I think it will
print "20" for little-endian and "0" for big-endian,
regardless of sizeof(int).

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

sophia
Guest
Posts: n/a

 03-07-2008
On Mar 7, 12:30 pm, Ian Collins <(E-Mail Removed)> wrote:
> sophia wrote:
> > 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

>
> No, it will not. p points the the second int in the array, byte order
> (or sizeof int ) has no effect.
>
> > how correct is the above answer ?

>
> Incorrect.
>

I have checked the answer on little endian machines using the
following
compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
and the answer seems to be correct

But I haven't checked the answer on a big endian machine

but as per the diagram given here:- http://www.df.lth.se/~pi/endian.html
the answer should be correct with the modification given
by Robbie Hatley (int)(*((char*)a + sizeof(int)));

Richard Heathfield
Guest
Posts: n/a

 03-07-2008
sophia said:

<snip>

> I have checked the answer on little endian machines using the
> following
> compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
> and the answer seems to be correct
>
> But I haven't checked the answer on a big endian machine

Question: What is the Universal Gravitational Constant?
Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's 42. On
Jupiter, it's 6.

I have checked the answer on Earth, and the answer seems to be correct.

But I haven't checked the answer on Mars or Jupiter.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

sophia
Guest
Posts: n/a

 03-07-2008
On Mar 7, 3:31 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> sophia said:
>
> <snip>
>
> > I have checked the answer on little endian machines using the
> > following
> > compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
> > and the answer seems to be correct

>
> > But I haven't checked the answer on a big endian machine

>
> Question: What is the Universal Gravitational Constant?
> Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's 42. On
> Jupiter, it's 6.
>
> I have checked the answer on Earth, and the answer seems to be correct.
>
> But I haven't checked the answer on Mars or Jupiter.

Thats really a great answer. you have a great humour sense.
Any way just to satisfy my curiosity

The value of Boltzmann constant is 1.380 6504(24) × 10-23 J/K
on Earth what will be its value on other inter dimensional worlds
if they exists OR at least in Mars ?

santosh
Guest
Posts: n/a

 03-07-2008
sophia wrote:

> On Mar 7, 3:31 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> sophia said:
>>
>> <snip>
>>
>> > I have checked the answer on little endian machines using the
>> > following
>> > compilers gcc 4.03, Dev -C++ ,turbo C++ 3.0
>> > and the answer seems to be correct

>>
>> > But I haven't checked the answer on a big endian machine

>>
>> Question: What is the Universal Gravitational Constant?
>> Answer: If we're on Earth, it's 6.673E-11 mmm/kg/ss. On Mars, it's
>> 42. On Jupiter, it's 6.
>>
>> I have checked the answer on Earth, and the answer seems to be
>> correct.
>>
>> But I haven't checked the answer on Mars or Jupiter.

>
> Thats really a great answer. you have a great humour sense.
> Any way just to satisfy my curiosity
>
> The value of Boltzmann constant is 1.380 6504(24) × 10-23 J/K
> on Earth what will be its value on other inter dimensional worlds
> if they exists OR at least in Mars ?

Did you understand what Richard wrote? The answer is the same as for G.