C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange pointer problems, now with ansi example

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Bruno van Dooren
Guest





PostPosted: Wed Jul 27, 2005 11:11 pm    Post subject: strange pointer problems, now with ansi example Reply with quote



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 main(int argc, char* 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%08xn",
array, ptrArray[0], element, *element);

printf("array[9] = %d, (*element)[9] = %d, element[9] = %dn",
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
}
int main(int argc, char* 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 an error,
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] = %dn", (*element)[9]);

ptrArray[0] = Array; //first element should be Array itself
element = (t_Array *)ptrArray[0];
printf("Weird Success: (*element)[9] = %dn", (*element)[9]);
}
int main(int argc, char* 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
the error, 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.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Thu Jul 28, 2005 11:58 am    Post subject: Re: strange pointer problems, now with ansi example Reply with quote



Bruno van Dooren wrote:
Quote:
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.

I don't believe that you turned on warnings then, because this


Quote:
printf("array = 0x%08x, ptrArray[0] = 0x%08x, element = 0x%08x,"
"*element = 0x%08xn",
array, ptrArray[0], element, *element);

is not suitable for outputting pointers. printf() uses %p for pointers while
this assumes ints. Since ints and pointers can have different sizes, all
bets are off that your program behaves even remotely well.

Quote:
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.

Could it be that you didn't take into account that an array automatically
'decays' to a pointer to the first element when passed to a function?

Quote:
typedef int t_Array[10];
void function(t_Array Array)
{
t_Array *ptrArray[] = {&Array}; //error
}

Hehe, that one's really mean. The things is, above function is like
void function( int Array[10])
but that's nothing else than
void function( int* Array)
IOW, you don't pass an array to the function, it is still just a pointer -
something which should be clear when carefully reading the error message
the compiler spits out.

My advice: don't try to treat arrays as types, wrap them in a struct to pass
them around.

Uli


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.