0

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
evolon696
  • 269
  • 1
  • 4
  • 13
  • 1
    `malloc(0)` will allocate 0 bytes of memory. – Seth Carnegie Nov 27 '11 at 02:15
  • 1
    When you say `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. – alxbl Nov 27 '11 at 02:16
  • This is a duplicate, see http://stackoverflow.com/questions/2575921/array-of-structs-in-c – charstar Nov 27 '11 at 02:37
  • You cannot use the standard `malloc()` and `realloc()` 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 as `str_malloc()` and `str_realloc()`). Note that names starting with `str` and a lower-case letter are reserved for future extensions of the standard C library. – Jonathan Leffler Nov 27 '11 at 03:06

1 Answers1

1

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.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • how would i realloc and be able to access array_of_strings[1]? – evolon696 Nov 27 '11 at 02:21
  • 1
    `array_of_strings = realloc(sizeof(string)*2);` – MByD Nov 27 '11 at 02:22
  • what if im doing char *string = (char*) malloc(strlen(string) + 1) am i reserving bytes or array slots? – evolon696 Nov 27 '11 at 02:25
  • I does not fit to the struct at all. – MByD Nov 27 '11 at 02:27
  • btw when i use my string struct it has to be a pointer does that mean i use 2 pointers or still use 1 like mbyd said to? i need an array of pointer structs – evolon696 Nov 27 '11 at 02:27
  • You cannot do `strlen(string)` because `string` is a type name and not a variable name. Besides, you do not need to use `strlen()`; you know how long the string is from the structure: `string *str; ...initialize...; unsigned len = str->size;`. – Jonathan Leffler Nov 27 '11 at 02:59