lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> =======[ a.c ]==========
> 
> int big_array[1024] = { 1 , -1 };
> 
> =======[ m.c ]==========
> 
> #include <stdio.h>
> 
> extern int *big_array;
> 
> int main(void)
> {
>  printf("%d %d\n",big_array[0],big_array[1]);
>  return 0;
> }
> 
>  There's a mismatch between the big_array instance and the big_array
> declaration.  

Sure, but isn't this what I have been saying? Arrays are just pointers
with syntactic sugar, and in the case of directly declared arrays the
compiler is just doing automatic dereferencing for you.

Lets match my example and change the declaration to:

int big_array[5] = { 5, 4, 3, 2, 1 };

And leave the declaration of 'big_array' as 'extern int *big_array'.

By the way, the variables in my example were declared exactly as the
ones in your original example, I just forgot to copy them over.

If we do a simple printf of the 'big_array' pointer:

printf("p[%p]\n", big_array);

We will get something like "p[0x400000005]" as the compiler is being
'helpful' and dereferencing things for us, because as you stated there
is a mismatch between the variable declaration and the extern instance.

However, you can still get the actual values by simply reversing what
the compiler is automatically doing behind the scenes:

printf("p[%p] 0[%d] 2[%d] 4[%d]\n", &big_array, *((int*)&big_array),
                                                *((int*)&big_array + 2),
                                                *((int*)&big_array + 4));

That messy line of code gives the following output:

p[0x10df46020] 0[5] 2[3] 4[1]

The ascii art you showed about arrays vs pointers is how we as programmers
would understand the difference between a pointer and a declared array.

To the compiler it's all the same, memory is allocated for the array and
a pointer is returned, the rest is semantics. If you have a pointer you
dereference it or use [], if you have declared an array directly then the
compiler will take care of the dereferencing for you behind the scenes.


>> I don't know what you mean by "the whole 2[pa] bit" though, can you explain?
> 
> #include <stdio.h>
> 
> char *a[] = { "zero" , "one" , "two" , "three" };
> 
> int main(void)
> {
>  puts( a[0] );
>  puts( 1[a] );
> 
>  putchar( 2[ 3[a] ] ); putchar('\n');
>  putchar( 2["0123"] ); putchar('\n');
> 
>  return 0;
> }
> 
>  Valid.  And it runs without error.

What the heck is this voodoo magic? I have never seen this syntax before.

A bit of cursory searching comes up with a StackOverflow page [1] explaining
how the result of this bit of magic is due to the spec not specifying that
the arguments to [] must be in any specific, or sane, order apparently!

You definitely taught me something new with that tidbit of information...
not that I think I'd ever use that seriously in any of my code!

Thanks for explaining your examples about arrays and pointers, even though
it does look like we were both on the same page... I just was thinking about
it more from the compilers point of view and not the programmers. ;)

~Paige

[1] https://stackoverflow.com/questions/5073350/accessing-arrays-by-indexarray-in-c-and-c