简体   繁体   中英

an array of structs?

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?

If you want to have an array of one string, you should allocate memory for one string:

string * array_of_strings;
array_of_strings = malloc(sizeof(string));

and then you may access array_of_strings[0] .

If you will declare it as string ** you actually declare a pointer to pointer to string , not a pointer to string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM