[openbeos] Re: Largely Offtopic C/C++ question about pointers

Hi

In the first example the interesting thing is that array and &array have the same value but a different type:

printf("0x%08x 0x%08x %d %d\n", array, &array, sizeof(array), sizeof(&array));

gives:

0xfcffc53c 0xfcffc53c 40 4

The exactly same applies for your element, where element and *element have the same value but are of different type:

printf("0x%08x 0x%08x %d %d\n", *element, element, sizeof(*element), sizeof(element));

gives:

0xfcffc53c 0xfcffc53c 40 4

Obviously you only want to access the array type.

In example 2 and 3 I came to the same conclusion as Jérôme. You are only passing a pointer here. You can check this by using:

        printf("%s %d\n", __PRETTY_FUNCTION__, sizeof(Array));

in the function in example 3 (with gcc).
This gives for your version:

void function(int *) 4
Weird Failure: (*element)[9] = 0
Weird Success: (*element)[9] = 321

If you change the function declaration to pass by reference:

void function(t_Array &Array)

you will get this result:

void function(int (&)[10]) 40
Weird Failure: (*element)[9] = 321
Weird Success: (*element)[9] = 321

and everything seems to work as expected.
With the same change you can get example 2 to work.

I can't tell you what exactly happens here either and why passing a pointer is a problem at all though.

Other related posts: