when i run this, it prints out the correct stuff, but then freezes? is this a segmentation fault?
int main(int argc, char** argv) {
int NumElements = 2; /* the number of elements in the string array */
String* string = (String*) malloc((sizeof (String*)) * NumElements); /* allocate some memory for the app to use */
unsigned int BytesReserved = ((sizeof (String*)) * NumElements); /* the amount of bytes reserved */
/* debug */
printf("Number of elements: %d\nAmount of bytes reserved: %u\n", NumElements, BytesReserved);
string[0] = String_Set("Hello, ");
string[1] = String_Set("world!");
/* loop through the array of strings and print each one */
int i;
for (i = 0; i < NumElements; i++) {
printf("%s\n", String_CString(string[i]));
}
free(string); /* deallocate the allocated memory we used earlier */
return 0;
}
Seems you allocate too less space for your “strings” if you wanted an array of strings you would need to first
allocate the array, then allocate for each string in the array
Now for each string in “string” you would need to allocate space that can contain the whole string you want to store:
Not sure what you do in String_Set though, maybe you are doing that