if (buffer[1] = '\n')
char ** myArray; malloc(*myArray, 2); //Initialise le nombre de ligne a 2 malloc(myArray[0], 15); // Initialise la premiere ligne a 15 caractères. strcpy(myArray[0], "bonjour"); // Affecte une valeur a la premiere ligne malloc(myArray[1], 15); // Initialise la premiere ligne a 15 caractères. strcpy(myArray[1], "au revoir"); // Affecte une valeur a la deuxieme ligne
#include void displayArray(char ** arr); int main(int argc, char **argv) { char **myArray = NULL; myArray = (char**)malloc(2); myArray[0] = (char*)malloc(15); strcpy(myArray[0], "bonjour"); myArray[1] = (char*)malloc(15); strcpy(myArray[1], "au revoir"); displayArray(myArray); system("pause"); return EXIT_SUCCESS; } void displayArray(char ** arr) { std::cout << "Table values :" << std::endl; for(int i=0;i<2;++i) { std::cout << arr[i] << std::endl; } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h void my_putchar(char c) { write(1, &c, 1); } int check(int fd, char *argv, int argc) { if (argc != 2) { my_putchar('\n'); my_putstr("Attention: Il y a trop de parametres !\n"); my_putchar('\n'); return (0); } if (fd == -1) { my_putchar('\n'); my_putstr("Erreur: Le fichier d'entree n'a pas ete trouve !\n"); my_putchar('\n'); return (0); } else return (1); } void aff_string(char **tabl) { int i; int j; i = 0; while (tabl[i] != '\0') { j = 0; while (tabl[i][j] != '\0') { my_putchar(tabl[i][j]); j = j + 1; } i = i + 1; } } int my_open_file(char *argv, char **tabl, int fd, int argc) { int x; int y; int reader; char *buffer; x = 0; y = 0; reader = 1; fd = open(argv, O_RDONLY); if (check(fd, argv, argc) == 0) return (-1); buffer = malloc(1 * sizeof(*buffer)); read(fd, buffer, 2); tabl = (char**)malloc(buffer[0]); tabl[y] = (char*)malloc(64); while (reader == 1) { reader = read(fd, buffer, 1); buffer[reader] = '\0'; if (buffer[0] == '\n') { y = y + 1; tabl[y] = (char*)malloc(64); x = 0; } tabl[y][x] = buffer[0]; x = x + 1; } aff_string(tabl); free(buffer); close(fd); } int main(int argc, char **argv) { char **tabl; int fd; int tmp; tmp = my_open_file(argv[1], tabl, fd, argc); if (tmp == -1) return (-1); //aff_string(tabl); }