1

That is the code:

int qtd_alunos, qtd_temas;
char* tnome[1][10];
char* anome[1][10];

printf("\nPra distribuir os temas me diga quantos alunos vao participar e aperte enter, depois a quantidade de temas.");
scanf("%d%d", &qtd_alunos, &qtd_temas);
printf("Agora vai escrevendo o nome de cada tema\n");

for(int j = 0; j<qtd_temas; j++){
    printf("Tema %d\n", j+1);
    scanf("%s", tnome[0][j]);
    printf("%s ok!\n",tnome[0][j]); }
return 0; }

When I run the code the program saves just the position "tnome[0][0]", but when the "int j" changes to [0][1] the program closes. I neeed to understand why it happens.

Yahya
  • 13,349
  • 6
  • 30
  • 42
  • [here](https://stackoverflow.com/questions/2614249/dynamic-memory-for-2d-char-array) is the answer, also [this](https://stackoverflow.com/questions/18083984/how-to-store-and-then-print-a-2d-character-string-array) answer will be enough. – Yahya Nov 02 '17 at 20:05
  • when asking about a run time problem, as this question is doing, post a [mcve] – user3629249 Nov 03 '17 at 15:22
  • when calling any of the `scanf()` family of functions: 1) always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the '%s' or '%[...]' input conversion specifiers, always include a MAX CHARACTERS modifier that is one less than the length of the input field. One less because `scanf()` always appends a NUL byte for those specifiers. – user3629249 Nov 03 '17 at 15:31

2 Answers2

1

You need to allocate space for your strings before you read them!

Use malloc() to do so (just before reading the input for example), or declare 3D arrays, instead of 2D.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

You are asking scanf to save a string into a pointer that is uninitialized. It points nowhere. To fix this, you need to fist allocate some memory manually for your string and then set the pointers. For example:

#define MAX_LEN 101

char *names[10];

for (int i = 0; i < 10; i++)
    names[i] = malloc(sizeof(char) * MAX_LEN);

I'm not sure why you're using a multidimensional array when the first dimension is of size 1. These 2D arrays could be easily reduced to 1D. If you meant to use it as a 1D array of strings (2D of chars) then use instead:

char names[10][MAX_LEN];

Here the first dimension is the amount of strings and the second is the size of them. And since it is automatically allocated, you don't need to do it yourself.

Two important remarks:

  • Enable compiler warnings.
  • Be careful with buffer overflow: you can limit the size of the string read by scanf in the format string (i.e. "%100s")
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • I removed MAX_LEN and I changed to: tnome[j] = malloc(sizeof(char)); Now it's working very well, and I am using a unidimensional array. – Thiago Barboza Nov 02 '17 at 21:08
  • @ThiagoBarboza you malloc’ed space for a single char. The example `MAX_LEN` was just the size of the string. Use whatever suits your needs. – sidyll Nov 02 '17 at 21:09
  • 1
    I got it. In case I need to write a bigger word at console the program can return an error. Thanks for the tip. – Thiago Barboza Nov 02 '17 at 21:15