C advice requested
Hi,
I have a simple question, hopefully with a simple answer. I have a structure: Code:
struct INFO Code:
bool updateInfo(struct INFO* pInfo) Any help is appreciated. Indy |
Use the greatest possible flexibility:
In updateInfo(), if the pointer is non-null, assume it has been allocated, else allocate storage from malloc()/calloc(). Wherever the storage is allocated, assign NULL to the pNext member. --- rod. |
Uh, no.
The way you have it designed, the caller *must* allocate the space; the callee *cannot* allocate it: Code:
Two alternatives might be: a) make pInfo a pointer to a pointer ("struct INFO **ppInfo") ... or ... b) make "udpateInfo()" return the pointer (instead of a "bool"). If your function simply "updates existing data", then like your original design better. Just allocate the struct before you call "updateInfo()", and Life is Good. Otherwise, if your function is actually "add new member to list", then I'd allocate inside the function ... and pass a pointer to a pointer. Here's an example: http://vergil.chemistry.gatech.edu/r...ial/lists.html IMHO .. PSM |
From what I can tell he is just updating the info in an existing list element in that case you could just modify the info directly. But then you ask about allocating memory and that is confusing as you wouldn't need to.
|
Thanks lads,
actually, paulsm4, i am allocating a new structure inside the updateInfo. So basically, I'm adding a structure inside a structure like: Code:
struct DATA Indy |
All times are GMT -5. The time now is 05:12 PM. |