[ Foro de C ]

Error al intentar guardar en un arreglo de apuntadores

10-Dec-2021 21:35
Carlos Alberto Sánchez
0 Respuestas

Hola que tal, espero puedan ayudarme, estoy intentando guardar un grupo de cadenas en arreglos, pero me marca error,según yo esta bien el intento pero no se que pasa. Mi ejemplo es:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4
 5 int main(int argc, char *argv[])
 6 {
 7     char str2[] = "Hola\tMundo";
 8     char str3[] = "\t";
 9     char *str1;
10     char (*token)[9];
11     int j;
12
13     for (j = 1, str1 = str2; ; j++, str1 = NULL) {
14         token[j] = strtok(str1, str3);
15         if (token[j] == NULL)
16             break;
17         printf("%d: %s\n", j, token[j]);
18     }
19    
20     exit(EXIT_SUCCESS);
21 }

y el error que me lanza es:

gcc main.c -o m.out
main.c: In function ‘main’:
main.c:14:18: error: assignment to expression with array type
        token[j] = strtok(str1, str3);
el programa si lo ejecuto con estos cambios, si corre normalmente (solo cambio el "token", un array de apuntadores a un apuntador solo).

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4
 5 int main(int argc, char *argv[])
 6 {
 7     char str2[] = "Hola\tMundo";
 8     char str3[] = "\t";
 9     char *str1;
10     char *token;
11     int j;
12
13     for (j = 1, str1 = str2; ; j++, str1 = NULL) {
14         token = strtok(str1, str3);
15         if (token == NULL)
16             break;
17         printf("%d: %s\n", j, token);
18     }
19    
20     exit(EXIT_SUCCESS);
21 }

output:
hola:atodos$./m.out
1: Hola
2: Mundo




(No se puede continuar esta discusión porque tiene más de dos meses de antigüedad. Si tienes dudas parecidas, abre un nuevo hilo.)