I am trying to build a list of strings that I need to pass to a function expecting char **
How do I build this array? I want to pass in two options, each wi
char **options[2][100];
declares a size-2 array of size-100 arrays of pointers to pointers to char
. You'll want to remove one *
. You'll also want to put your string literals in double quotes.
How to create an array size 5 containing pointers to characters:
char *array_of_pointers[ 5 ]; //array size 5 containing pointers to char
char m = 'm'; //character value holding the value 'm'
array_of_pointers[0] = &m; //assign m ptr into the array position 0.
printf("%c", *array_of_pointers[0]); //get the value of the pointer to m
How to create a pointer to an array of characters:
char (*pointer_to_array)[ 5 ]; //A pointer to an array containing 5 chars
char m = 'm'; //character value holding the value 'm'
*pointer_to_array[0] = m; //dereference array and put m in position 0
printf("%c", (*pointer_to_array)[0]); //dereference array and get position 0
How to create an 2D array containing pointers to characters:
char *array_of_pointers[5][2];
//An array size 5 containing arrays size 2 containing pointers to char
char m = 'm';
//character value holding the value 'm'
array_of_pointers[4][1] = &m;
//Get position 4 of array, then get position 1, then put m ptr in there.
printf("%c", *array_of_pointers[4][1]);
//Get position 4 of array, then get position 1 and dereference it.
How to create a pointer to an 2D array of characters:
char (*pointer_to_array)[5][2];
//A pointer to an array size 5 each containing arrays size 2 which hold chars
char m = 'm';
//character value holding the value 'm'
(*pointer_to_array)[4][1] = m;
//dereference array, Get position 4, get position 1, put m there.
printf("%c", (*pointer_to_array)[4][1]);
//dereference array, Get position 4, get position 1
To help you out with understanding how humans should read complex C/C++ declarations read this: http://www.programmerinterview.com/index.php/c-cplusplus/c-declarations/
I think what you originally meant to do was to make an array only of characters, not of pointers:
char options[2][100];
options[0][0]='t';
options[0][1]='e';
options[0][2]='s';
options[0][3]='t';
options[0][4]='1';
options[0][5]='\0'; /* NUL termination of C string */
/* A standard C library function which copies strings. */
strcpy(options[1], "test2");
The code above shows two distinct methods of setting the character values in memory you have set aside to contain characters.
C strings are enclosed in double quotes:
const char *options[2][100];
options[0][0] = "test1";
options[1][0] = "test2";
Re-reading your question and comments though I'm guessing that what you really want to do is this:
const char *options[2] = { "test1", "test2" };