I'm currently working on some homework that is suppose to be a library of functions that make something like a non-null terminated string. It was due yesturday, and I'm all out of ideas for the last part though. As far as I can tell, the code all works except when I need to update elements of a struct while inside of a function. I've found this is a problem to be an issue with the function using a copy of the struct and not updating the actual struct. pass by value.. Well the only fixes that I could find for this kind of problem involved using a pointer to a pointer in the functions, but this won't work in my program because I have a typedef that I have to use, and the functions are already declared, I just need to fill in the body and also make the struct itself.
If anyone could help me figure out a way to update the actually struct while inside of the functions, that would be awesome. Here's the struct, the malloc, and free fucnction, as well as the typedef from the header file. Out of these, the only problem I see is in the free function. When x is freed, but but actually set to null after the return. I also have a bunch of other functions that I already wrote, but they have the same problem, not updating after the function ends. Any help would be great, thanks
Code:typedef struct foo * Foo; struct foo{ size_t currentSize; size_t maxSize; char string[1]; }; Foo FooMalloc(size_t size){ Foo x = (Foo)malloc(sizeof(struct foo) + ((2 * size) * sizeof(char))); if(x == NULL){ fprintf(stderr, "Not enough memory, exiting program"); exit(100); } x->currentSize = 0; x->maxSize = 2 * size; return x; } void FooFree(Foo x){ if (x == NULL){ /* Foo has already been freed */ } else { free(x); x = NULL; } }