strange pointer behavior

  • Thread starter Thread starter Bruno van Dooren
  • Start date Start date
B

Bruno van Dooren

Hi All,

i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.

----------------------------------------------
//example 1:
typedef int t_Array[10];
int _tmain(int argc, _TCHAR* argv[])
{
t_Array array;
array[0] = 123;
array[9] = 321;

t_Array *ptrArray[] = {&array};
t_Array *element = ptrArray[0];

printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x, *element =
0x%08x\n",
array, ptrArray[0], element, *element);

printf("array[9] = %d, (*element)[9] = %d, element[9] = %d\n",
array[9], (*element)[9], element[9]);

return 0;
}

now according to the print statements, the different pointers all point to
the same thing, even though 'element' is dereferenced at one place, and not
dereferenced at another place. if i try to print a value from the array, i
get 2 different results.
??
----------------------------------------------
example 2:
typedef int t_Array[10];
void function(t_Array Array)
{
t_Array *ptrArray[] = {&Array}; //error C2440
}
int _tmain(int argc, _TCHAR* argv[])
{
t_Array array;
t_Array *ptrArray[] = {&array}; //no error
return 0;
}
the 2 declarations of the arrays look the same to me, but 1 gives error
C2440, and the other one doesn't. i have looked up the documentation of that
error, but that didn't shine much light.
??

----------------------------------------------
example 3:
typedef int t_Array[10];
void function(t_Array Array)
{
t_Array *element = NULL;
void * ptrArray[] = {NULL};

ptrArray[0] = &Array; //first element should be pointer to a t_Array
element = (t_Array *)ptrArray[0];
printf("Weird Failure: (*element)[9] = %d\n", (*element)[9]);

ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %d\n", (*element)[9]);
}
int _tmain(int argc, _TCHAR* argv[])
{
t_Array array;
array[9] = 321;
function(array);
return 0;
}
this is what really caused my headaches. i used void pointers to get rid of
error C2440, but that caused weird behavior. in the first situation in
'function', i expect to dereference a pointer to end up with an array, but
that clearly is not what happens.
in the second situation i expect to do something illegal: ie to use a
pointer to an array as the array itself, and somehow that seems to work.
what do i miss here?

i would be very grateful for some light on this murky behavior (or at least
my murky understanding).

kind regards,
Bruno.
 
Well, I don't know why you wish to do things like this in the first place.

I could shed some light to some of your problems.
Statements like

int *var[];

is defining a pointer to an array, but this is not desirable when you are
already working with an array. If you know the basic concept of arrays in
c/c++ then you would know that arrays by nature are pointers anyway.
So if you did something like this.
int *a = {1,2,3,4};
int b[]={1,2,3,4};
they are in fact identical.
You are infact defining an array of arrays whith some of your statements. If
you want to get a pointer to an array that already exists then you would need
something like the following.

int a[10];
int *b;
b = a; //might be wrong, if it is use b = &a[0]

Also, instead of using the typedef, try defining the arrays normally, that
may help with some of your problems too.
 
what i want to do is to use an array of pointers to arrays (depending on the
situation i need to use different arrays).
and i use the typedefs because the arrays are used in different modules, and
i like the extra type checking.

so i expect
int *var[];

to be an array of pointers.

kind regards,
Bruno.
 
For example 1, i think that its the definitions at the time that printf is
running is the problem. Using the debugger there is no problems at all. But
the thing that may cause problems, when you dereference element you will end
up with an integer array, so you may be causing problems with that. Since
your code shows that (*variable)[0] gives you correct results then go with
that usage. The possible problem there is that when its not dereferenced, it
will read more than it should because its expecting an int* and not an int[].

For the second problem, its because the parameter is being passed by value
not reference, change the function definition to

void fun(t_Array& array)
{
t_Array *ptrArray[] = {&array};
}

and you will be ok.

For the third example, since I fixed it with the second example I don't
think I need to go into it for now.
 
Back
Top