Need to get some information from the user.

#include <stdio.h> #include <stdlib.h> #include <windows.h> int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); printf("Π’Π²Π΅Π΄ΠΈΡ‚Π΅: ВСкст, \n"); int index[]; char user[]; char name[]; int i=0; while(scanf("%s,%d,%s",&name,&index,&user)==1) { scanf("%s %d %s",name[i], index[i], user[i]); i++; } } 

But the compiler gives an error

 error: array size missing in 

For some reason, requires explicitly specify the size. As I understand it, it is better to store this data in a structure, but only one array seems to be allowed there without specifying its size.

  • 3
    Why is the question tagged with [C] and [C ++] at the same time? So C or C ++? - AnT September
  • What is scanf("%s,%d,%s",&name,&index,&user) followed by suddenly scanf("%s %d %s",name[i], index[i], user[i]) . What is there in general mean? - AnT September
  • IMHO, in this case a dynamic list of structures would be better suited {int index; char user[USER_MAX]; name[NAME_MAX]; } {int index; char user[USER_MAX]; name[NAME_MAX]; } {int index; char user[USER_MAX]; name[NAME_MAX]; } - PinkTux 4:41 pm

2 answers 2

For some reason, requires explicitly specify the size.

Of course, requires. In C, there are no, and never have been, named arrays of varying size at runtime. The size of the named array in C must be known in advance , at the time of its definition. This size may be the amount of execution time, but it should be known by the time the array is defined. Point.

If you need an array of variable size, then there is only one solution - manual allocation of the array in the dynamic memory (and re-allocation, if necessary, increase the size).

    to what @AnT said

    there is an initialization error in your example

     int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int num; // Ρ€Π°Π·ΠΌΠ΅Ρ€ массива cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ€Π°Π·ΠΌΠ΅Ρ€ массива: "; cin >> num; // ΠΏΠΎΠ»ΡƒΡ‡Π΅Π½ΠΈΠ΅ ΠΎΡ‚ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ Ρ€Π°Π·ΠΌΠ΅Ρ€Π° массива printf("Π’Π²Π΅Π΄ΠΈΡ‚Π΅: ВСкст, \n"); // динамичСскоС созданиС ΠΎΠ΄Π½ΠΎΠΌΠ΅Ρ€Π½Ρ‹Ρ… массивов Π½Π° num элСмСнтов int *index[] = new int[num]; char *user[] = new char[num]; char *name[] = new char[num]; int i=0; while(scanf("%s,%d,%s",&name,&index,&user)==1) { scanf("%s %d %s",name[i], index[i], user[i]); i++; } }