473,411 Members | 1,880 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

strange pointer behavior

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.
Nov 17 '05 #1
3 907
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.
Nov 17 '05 #2
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.

"D.Rowe" <DR***@discussions.microsoft.com> wrote in message
news:30**********************************@microsof t.com...
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.

Nov 17 '05 #3
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.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruno van Dooren | last post by:
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. ...
3
by: Bruno van Dooren | last post by:
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. ...
2
by: Bruno van Dooren | last post by:
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. ...
2
by: Pinux | last post by:
Hi, I now run into a very strange behavior when using ifstream. Can anyone please help me out here: Basically, what I want to do is to encrypt a plain text file into a cipher text and then...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.