whatdoido.c

A

aarklon

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 = &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.
 
A

Alexei A. Frounze

int main(void)
{

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

for(i=0;i<5;i++)
a = &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
 
N

nick_b20

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 = &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
 
C

Christopher Benson-Manica

int main(void)
{
int a[6] = {1,2,3,4,5,6},i;
for(i=0;i<5;i++)
a = &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".
 
E

Emmanuel Delahaye

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 = &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
 
A

aarklon

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top