Pointer to a function parameter
ATL / WTL / STL
8
Posts
2
Posters
0
Views
1
Watching
-
I write a function like:
void func1(char chx[], int y)
{
int wsn = 0;wsn = \*(int \*) (&chx); if (wsn == 0) { ... }
}
Compiler works well, no warning, no error. But when the code is running, seems it get a wild pointer. the code crashed.
-
Should be:
void func1(char chx[], int y)
{
int wsn = 0;wsn = \*(int \*)chx; if (wsn == 0) { ... }
}
The variable name
chx
is already a pointer to the array, adding theaddressof
operator creates a pointer to that pointer. -
No it doesn't. I have no idea how you tested this but it is not correct. Build the following, step through it with your debugger and you will see that they are different.
int\* pAddressOf = (int \*) (&chx); int\* pNormal = (int \*)(chx);
-
No it doesn't. I have no idea how you tested this but it is not correct. Build the following, step through it with your debugger and you will see that they are different.
int\* pAddressOf = (int \*) (&chx); int\* pNormal = (int \*)(chx);