Originally Posted by ta0kira
Who knows what it will do to your program, but it can't be good.
I really got to thinking about what I said here. Here is what I really think would happen if you passed a char(*)
and overwrote it in a function (removed 1 x *
Suppose you allocate a char
on the stack. It would look something like this:
When passing this array to a function, you will just be passing the value of its location, which is 0x10
. This value is just a number and has no location.
If you try to pass a pointer to the char
, you have no address to take since 0x10
is just a number. In order for it to have a location, it needs to be converted to an lvalue by placing it on the stack:
0x10 0x1a 0x1b
| | |
... [~~~~~~~~~~~] [0x10]
This is just a copy of the pointer, so the value 0x1b
is passed to the function. When it's dereferenced in the assignment, you are left with an lvalue of the copy on the stack, so any change to the pointer will be lost when the function returns. It's therefore highly pointless to pass a pointer to a fixed array since it doesn't provide any functionality not available when just passing the array. The only thing char*(*)
gives you over char*()
is the requirement for an additional NULL check unless you literally have a char*(*)
you want to change.
PS If you are wondering, taking char string
as an example, why the address of string
isn't just passed since "it's a variable which holds a pointer to the array", here is my answer. string
a variable, but a hard-wired value which represents an offset from the point on the stack where the function will return. If you created the variable char(*pointer) = &string
on the stack, however, pointer
would be a valid value to pass and can legitimately be changed.