I am trying to write up a string container for my string struct but it's not working. I feel like I've been showing everyone my code and expecting a simple answer to get me going but what I'd like is a tip or a few pointers to get me going. Right now I don't want this container to be able to hold anything else except a custom string that I wrote earlier, which is working fine by the way.
All the code is a simplified version of my real string struct because there's no need to post it; all we're dealing with is the string container.
header.h
typedef struct string string;
source.c
struct string {
char *buffer;
unsigned int size;
};
Would I do:
string ** array_of_strings;
or
string * array_of_strings;
then I want to do something like:
client.c
array_of_strings = (string *) malloc(0);
When I call malloc(0), I am wanting there to be array_of_strings[0]
and if I realloc(1) I would like it to be array_of_strings[1].
Is there a better way to do this, because this isn't working?
malloc(0)will allocate 0 bytes of memory.malloc(0)you are saying: "Allocate 0 bytes of space on the heap." You want to allocate at least sizeof(string). You also want to allocate the buffer that the string struct points to.malloc()andrealloc()functions as you show; you could write your own with the non-standard semantics you request (but you would have to use different names, such asstr_malloc()andstr_realloc()). Note that names starting withstrand a lower-case letter are reserved for future extensions of the standard C library.