Donner un tableau 2D de char* en parametre
Bonjour à tous,
Je cherche à créer une fonction d'initialisation d'un tableau à deux dimensions de char*. Le prototype de la fonction est le suivant :
Code:
1 2 3 4 5 6
| void init_array(char** my_array, /* Pointeur vers le tableau à initialiser */
int nb_of_lines, /* Nombre de lignes du tableau */
int nb_of_rows, /* Nombre de colonnes du tableau */
int size_of_pointer, /* Nombre d'octets pour la taille du pointeur */
char* init_char /* Caractère d'initialisation */
); |
Et son utilisation dans un programme se ferait ainsi :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <stdio.h>
#include <stdlib.h>
#include <tchar.h>
#include <string.h>
#define NB_OF_LINES 10
#define NB_OF_ROWS 20
#define SIZE_MAX_FUNCTION_NAME 5
void init_array(char** my_array, int nb_of_lines, int nb_of_rows, int size_of_pointer, char* init_char);
int main (void)
{
char *tableau[NB_OF_LINES][NB_OF_ROWS];
init_array(*tableau,NB_OF_LINES,NB_OF_ROWS,SIZE_MAX_FUNCTION_NAME,"*");
return 1;
}
void init_array(char** my_array, int nb_of_lines, int nb_of_rows, int size_of_pointer, char* init_char)
{
int temp_index_i = 0;
int temp_index_j = 0;
for(temp_index_i=0;temp_index_i<nb_of_lines;temp_index_i++)
{
for(temp_index_j=0;temp_index_j<nb_of_rows;temp_index_j++)
{
my_array[temp_index_i][temp_index_j] = (char*)malloc(size_of_pointer*sizeof(char));
memcpy(&my_array[temp_index_i][temp_index_j],init_char,size_of_pointer);
}
}
} |
Cependant à la compilation (Microsoft Visual C++ 2008), on me renvoie une erreur sur la ligne :
Code:
my_array[temp_index_i][temp_index_j] = (char*)malloc(size_of_pointer*sizeof(char));
disant error C2440: '=' : cannot convert from 'char *' to 'char'.
Je comprends ce qu'il me dit mais je ne vois pas en quoi ce que je fais est une erreur et surtout comment le résoudre. Une solution pour m'aider à initialiser mes pointeurs s'il vous plait ?
Merci d'avance ! :ccool: