473,473 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

whatdoido.c

Hi all,

i found the following file whatdoido.c at
www.cs.toronto.edu/~hsc/270/examples/

on running the question 3)

i.e
int main(void)
{

int a[6] = {1,2,3,4,5,6},i;

for(i=0;i<5;i++)
a[i] = &a[i+1];
printf("%d\n",******(int******)a);
return 0;
}

i am getting the answer as 6(on dev c++ 4 compiler). can anybody
explain why this is so.
i am not an expert by any means,so please be kind.

Nov 15 '05 #1
5 1527
<aa*****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
int main(void)
{

int a[6] = {1,2,3,4,5,6},i;

for(i=0;i<5;i++)
a[i] = &a[i+1];
printf("%d\n",******(int******)a);
return 0;
}

i am getting the answer as 6(on dev c++ 4 compiler). can anybody
explain why this is so.
First of all, the above code is broken in terms of portability because you
store data pointers into integers.
And there's no type casting either. Doesn't the compiler tell anything like
"warning: initialization makes integer from pointer without a cast"?

If this is assignment given by your prof, you must give him some bad credit
as well, it's not only him to grade others :)
i am not an expert by any means,so please be kind.


Well, do your homework yourself. With a little bit of thinking you'll find
why the above does what it does on your platform with your compiler. Just
write down the contents of a[] after the for loop. And then apply those *
one by one to a and you'll see why 6.

Alex
Nov 15 '05 #2
Hello,

I should say I am seeing this kind of stuff for the first time.

Basically, it changes the array of data integers (namely A) to an array
of (address integers+last elemnet as data integer). Please see the
picture below:

1.) statement: int a[6] = {1,2,3,4,5,6},

A
addresses values

0000 1
0004 2
0008 3
0012 4
0016 5
0020 6
2.) statement: for(i=0;i<5;i++) a[i] = &a[i+1];

A
addresses values

0000 0004
0004 0008
0008 0012
0012 0016
0016 0020
0020 6

3.) Now

*A = *(0000) = 0004
**A = *(0004) = 0008
***A = *(0008) = 0012
****A = *(0012) = 0016
*****A = *(0016) = 0020
******A = *(0020) = 6

Hence, the result
Thanks,
Nikhil
aa*****@gmail.com wrote:
Hi all,

i found the following file whatdoido.c at
www.cs.toronto.edu/~hsc/270/examples/

on running the question 3)

i.e
int main(void)
{

int a[6] = {1,2,3,4,5,6},i;

for(i=0;i<5;i++)
a[i] = &a[i+1];
printf("%d\n",******(int******)a);
return 0;
}

i am getting the answer as 6(on dev c++ 4 compiler). can anybody
explain why this is so.
i am not an expert by any means,so please be kind.


Nov 15 '05 #3
aa*****@gmail.com wrote:
int main(void)
{
int a[6] = {1,2,3,4,5,6},i;
for(i=0;i<5;i++)
a[i] = &a[i+1];
The real "quiz" contains a cast here, but it's still wrong.
printf("%d\n",******(int******)a);
return 0;
} i am getting the answer as 6(on dev c++ 4 compiler). can anybody
explain why this is so.


It dumps core for me *shrug*. It's a broken "quiz".

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #4
aa*****@gmail.com wrote on 16/09/05 :
i found the following file whatdoido.c at
www.cs.toronto.edu/~hsc/270/examples/

on running the question 3)

i.e
int main(void)
{

int a[6] = {1,2,3,4,5,6},i;

for(i=0;i<5;i++)
a[i] = &a[i+1];
Undefined behaviour : there is no guarantee that an int is big enough
for an address.
printf("%d\n",******(int******)a);
Undefined behaviour : Missing <stdio.h> header for printf().
Undefined behaviour : dereferencing of undefined pointers.

return 0;
}

i am getting the answer as 6(on dev c++ 4 compiler). can anybody
explain why this is so.
i am not an expert by any means,so please be kind.


This code is broken. Don't do that.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
Nov 15 '05 #5
Hi all,

thanks for the reply. but there was a mistake in the posting as
christopher-benson pointed out the real question contains a integer
cast

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.