Dynamic string array struct in C
I have to write a function in c which will return a dynamic array of strings. Here are my requirements:
- I have 10 different examine functions which will retur开发者_如何转开发n either true or false and associated error text. (error text string is also dynamic).
- My function must collect the result(true or false) + the error string and it will be called n examine functions. So my function must collect n results and finally return a dynamic array of strings to other functions.
You can allocate an array of arbitrary length with malloc() (it's like "new" in Java), and make it grow or shrink with realloc().
You have to remember to free the memory with free() as in C there is not garbarage collector.
Check: http://www.gnu.org/software/libc/manual/html_node/Memory-Allocation.html#Memory-Allocation
Edit:
#include <stdlib.h>
#include <string.h>
int main(){
char * string;
// Lets say we have a initial string of 8 chars
string = malloc(sizeof(char) * 9); // Nine because we need 8 chars plus one \0 to terminate the string
strcpy(string, "12345678");
// Now we need to expand the string to 10 chars (plus one for \0)
string = realloc(string, sizeof(char) * 11);
// you can check if string is different of NULL...
// Now we append some chars
strcat(string, "90");
// ...
// at some point you need to free the memory if you don't want a memory leak
free(string);
// ...
return 0;
}
Edit 2: This is the sample for allocate and expand an array of pointers to chars (an array of strings)
#include <stdlib.h>
int main(){
// Array of strings
char ** messages;
char * pointer_to_string_0 = "Hello";
char * pointer_to_string_1 = "World";
unsigned size = 0;
// Initial size one
messages = malloc(sizeof(char *)); // Note I allocate space for 1 pointer to char
size = 1;
// ...
messages[0] = pointer_to_string_0;
// We expand to contain 2 strings (2 pointers really)
size++;
messages = realloc(messages, sizeof(char *) * size);
messages[1] = pointer_to_string_1;
// ...
free(messages);
// ...
return 0;
}
Consider creating apropriate types suitable for you problem. For example, you can create a struct holding a pointer and sn integer length to represent the dynamic arrays.
Do you have some constraints over the prototyping of the
examine()
function and the function you have to write ? (let's call itvalidate()
)You say you have 10
examine()
functions, does it mean you will have a maximum of 10 messages/results in the array return byvalidate()
?
I'm a Java programmer with a C background, so maybe I can highlight a few things for you :
there is no equivalent of Array.length in C : you'll have to supply a side integer value to store the effective size of your array
C arrays can't "grow" : you'll have to use pointers and allocate/reallocate the memory pointed by your array begin pointer as this array grows or shrinks
you should already know that there is no notion of class or method in C, however you can use
struct
,typedef
and function pointers to add some kind of object oriented / genericity behavior to your C programs...Depending on your needs and obligations, arrays might be a good way to go, or not : perhaps you should try to figure out a way of building/finding an equivalent of the java List interface in C, so that you can add, remove/destroy or sort examine result elements without having to duplicate memory allocation / reallocation / freeing code each time you manipulate your result set (and you should perhaps send a header file with your structs/examine functions to describe what you did for now anyway, and express your needs a bit more precisely, so that we can guide you to the good direction)
Don't hesitate to provide more information or ask for specifics about the above bullets points ;)
精彩评论