PE> void brilliant_function(char **p)
PE> {
PE> static char names[3][20] = { "bob", "rod",
"paul");
PE>
PE> *p = names[2];
PE> return;
PE> }
PE> int main(void)
PE> {
PE> char *n;
PE>
PE> brilliant_function(&n);
PE> printf("the brilliant person is %s\n", n);
PE> return (0);
PE> }
PE> Well, as you can see, the caller is only declaring a pointer
PE> himself. That pointer is pointing to some random location in
PE> memory.
BL> Couldn't you have done the same thing by returning the pointer from
BL> brilliant_function()..
Yes you could. If you only have one return value, that will work. Or you
might be setting an array of pointers, etc.
BL> . or just pass (n) instead of (&n) and write
BL> the data to that location?
I told you, n is pointing to a random location. Writing to a random
location causes random things to become corrupted.
BL> I'm not saying that you can't make it work with pointers to
BL> pointers; just that I can't see any point in it. If you are going to
BL> pass a pointer, then you may as well pass it back and use the one
BL> pointer, or point it at the actual data rather than a pointer that
BL> points to the data. It just seems to take an extra step for no reason.
BL> Unless you used a typed pointer and wanted to mix them...
It's not an extra step, it's the minimum you can do in that circumstance.
If you were to declare n as char[500], you could do a strcpy into that. A
strcpy() is far more expensive than setting a pointer.
BL> Does a static array persist after the function has been called? If
Yes.
BL> not, you'll be pointing to phantom data.
Good job it's not then, eh?
BL> In Pascal, you have to make
BL> it global outside the called function, or you lose it. And what
In Pascal, you can't even read files.
BL> happens if you call it again?
I don't know what you mean.
BL> Does it make a new set of data in memory?
No. That data is stored in a fixed position, set at compile time, and
built into the executable. That's the whole point of static.
BL> How does it know one "names" from the next one?
There's only the one. In that function, anyway. BFN. Paul.
@EOT:
---
* Origin: X (3:711/934.9)
|