1

following is the structure and I want to create array of this structure in C and initialize, but confuse with how to initialize char **input and char **output.

typedef struct _test_data_vector {
    char *api;
    char **input;
    char **output;
}vector_test_data;

following is what I tried.

typedef struct _test_data_vector {
    char *api;
    char **input;
    char **output;
}vector_test_data;

vector_test_data data[] = {
    {
        "vector_push_back",
        {"1","2","3","4","5","6","7","8","9","10"},
        {"1","2","3","4","5","6","7","8","9","10"}
    }
};
1
  • Well, initialize them to what? To an actual string table? Or set them simply to 0? Commented Feb 8, 2011 at 12:09

4 Answers 4

1

You're very close, just specify type through compound literals (starting from C99)

This what I've tested with no warnings:

typedef struct _test_data_vector {
    char *api;
    char **input;
    char **output;
}vector_test_data;

vector_test_data data[] = {
    {
        "vector_push_back",
        (char*[]){"1","2","3","4","5","6","7","8","9","10"},
        (char*[]){"1","2","3","4","5","6","7","8","9","10"}
    }
};

printf("TEST: %s", data[0].input[2]);

Otput: TEST: 3

Sign up to request clarification or add additional context in comments.

9 Comments

No this won't work. It won't be a 2-dimensional array, but rather some ugly typecast abomination. Did you try printf("TEST: %s", data[1].input[0]);
@Lundin Please revert back my vote, pay attention that you've just run out of data array bounds. It has only one element ! It's not odd at all, just consult this: Compound Literals
@Martin Why use pointer-to-pointer syntax if you didn't intend to use multiple dimenstions? If you had declared this correctly, you would have a [10][3] matrix, 10 dimensions with 3 chars in each. Also, this has nothing to do with compound literals, you'd get that same problem no matter, since a pointer-to-pointer is used.
Please read question with more attention first. I didn't change almost anything, and just respect author's needs. And don't confuse a multidimensional array with an array to a string !
The OP is very likely confused, as most programmers are about this subject... you will have to keep that in mind before giving an answer equally confusing. And as this is C, multidimensional arrays are identical to arrays of strings, it is just a different nomenclature for the very same thing.
|
0

be a little more specific , usually when you have to initialize a pointer to a pointer you do it like this:

char * charPointer;//this is the char you want the input to point at;
*input = charPointer;

I had to use pointer to pointer on a project to but if ther is any way you can avoid this it would be easyer

Comments

0

api, input, and output will, by default, be initialized to NULL. You can initialize api directly, but input and output must be allocated, either at compile time by defining arrays, or at runtime, perhaps via malloc. You really should provide more information about what you are trying to do in order to get a more helpful answer.

Comments

0
char text[] = "Input text";
char *output;
struct _test_data_vector {
    char *api;
    char **input;
    char **output;
};

struct _test_data_vector A = { NULL, &text, &output };

If you want to allocate space for them, you can also do:

struct _test_data_vector B = {
  "api",
  malloc( 5 * sizeof *B.input ),
  malloc( 10 * sizeof *B.output )
};

just make sure you check that the allocation was succesful. (I strongly advised against doing this, as it is much clearer to call malloc in non-initializer code.)

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.