web-dev-qa-db-fra.com

Initialisation du tableau de caractères 2D en C

J'essaie de construire une liste de chaînes que je dois passer à une fonction attendant char **

Comment puis-je créer ce tableau? Je veux passer en deux options, chacune avec moins de 100 caractères.

char **options[2][100];

options[0][0] = 'test1';
options[1][0] = 'test2';

Cela ne compile pas. Qu'est-ce que je fais mal exactement? Comment créer un tableau de caractères 2D en C?

9
Derek

Les chaînes C sont entre guillemets:

const char *options[2][100];

options[0][0] = "test1";
options[1][0] = "test2";

En relisant votre question et vos commentaires, je suppose que ce que vous vraiment voulez faire est le suivant:

const char *options[2] = { "test1", "test2" };
26
Paul R

Comment créer un tableau de taille 5 contenant des pointeurs vers des caractères:

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

Comment créer un pointeur vers un tableau de caractères:

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

Comment créer un tableau 2D contenant des pointeurs vers des caractères:

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.

Comment créer un pointeur vers un tableau 2D de caractères:

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

Pour vous aider à comprendre comment les humains doivent lire les déclarations C/C++ complexes, lisez ceci: http://www.programmerinterview.com/index.php/c-cplusplus/c-declarations/

10
Eric Leschinski
char **options[2][100];

déclare un tableau taille-2 de tableaux taille-100 de pointeurs vers des pointeurs vers char. Vous voudrez en supprimer un *. Vous voudrez également mettre vos littéraux de chaîne entre guillemets.

4
Fred Foo

Je pense que ce que vous vouliez faire à l'origine était de créer un tableau uniquement de caractères, pas de pointeurs:

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

Le code ci-dessus montre deux méthodes distinctes de définition des valeurs de caractères dans la mémoire que vous avez réservées pour contenir des caractères.

1
Heath Hunnicutt