i'm making a program in C that let the user insert the number of names that he wants, this names is stored in a global array and then they are printed, but the program finishes before, specifically when i try to access to the global array to print the names to show them to the user. I need to have my global array as follows: char *array[10]. This problem just happens when i use the previus syntax, but when i use: char array[10][], all runs fine, what's the problem here? somebody can help me please, i have tried so many hours.
CODE:
#include <stdio.h>
#define MAX 10
int counter = 0;
char *concts[MAX];
void add_2(char *name){
printf("Se anadira un elemento en el index %d\n", counter);
concts[counter] = name;
counter++;
}
void main(){
char *name;
int ingresando = 1, i;
do{
printf("ingresa un nombre: ");
scanf("%s", &name);
add_2(name);
printf("Seguir ingresando? ");
scanf("%d", &ingresando);
}while(ingresando == 1);
printf("Terminado. contador: %d\n", counter);
for(i = 0; i < counter; i++){
char *otherName = concts[i];
printf("%s\n", otherName);
}
}
PROBLEM: I don't really know, but the program ends before what is expected, it compiles well and does not prompt errors.
EDIT: The program stops after print "Terminado. contador: %d\n"
-Wall. Also you never initialisename.*concts[MAX]is an array of pointers those pointing to no allocated memory, so you should allocate memory for each pointer or just declare it for example likechar concts[MAX][20]nameis a pointer and can't store a string, use an array of chars instead.-Wall -Werrorto fix that false perception.mallocmemory for each string, or declare a 2D array. For examplechar concts[10][32]gives you room for 10 strings of 32 bytes each. Andchar *nameshould bechar name[32].