2

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 return 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.
2
  • 15
    So what have you tried so far? Commented Feb 13, 2011 at 0:57
  • 1
    can you give us the header of one of the examine funcs? Commented Feb 13, 2011 at 1:11

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

7 Comments

When using malloc set the size parameter to the # of elements * the sizeof(type)
Thanks fernando.If you can provide a sample code for this that would be great.
strcpy() is cancerous. Use strncpy() instead to avoid buffer overruns.
@Jonathan - Meh. memcpy is better. If you can guarantee nul-termination, strcpy is at worst a bit inefficient. If you can't guarantee termination and you know how long your data is, strncpy will be slightly more inefficient than memcpy. If you don't know how long your data is and/or may have imbedded nul characters, strncpy is just as dangerous/useless.
@Jonathan - strncpy is never appropriate. It doesn't guarantee nul termination if the buffer is too small for the data, and writes useless extra zeroes if the buffer is too large for the data. It's a lose-lose proposition. Use memcpy.
|
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.

Comments

0
  1. Do you have some constraints over the prototyping of the examine() function and the function you have to write ? (let's call it validate())

  2. You say you have 10 examine() functions, does it mean you will have a maximum of 10 messages/results in the array return by validate() ?

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.