-1

I want to compile the program with a dynamic size of the table I tried int Table[]; My program's

the compiler returns this message 4 C:\Documents and Settings\Administrateur\Mes documents\TD4.c storage size of `table' aren't known? I don't know storage? what is my fault

Zouhair Kasmi
  • 614
  • 2
  • 6
  • 13
  • Your fault is that you don't know which programming language you use: is it C or C++? Please adjust your tags appropriately and/or name your documents the way you need it. – glglgl Dec 27 '12 at 19:38
  • before, at the university, we use cpp extension to compile a c program . – Zouhair Kasmi May 13 '19 at 16:12

4 Answers4

4

In C, you can't declare an array with unknown size.

int Table[];

is simply not allowed.

Instead you can declare a pointer:

int *Table;

and dynamically allocate/reallocate using malloc/realloc functions.

P.P
  • 117,907
  • 20
  • 175
  • 238
  • You can declare array in that case. `int table[100];` Note that it can't a variable like `N`. Variable length arrays are supported only after C99. But `N` can be defined `#define N 100`. – P.P Dec 27 '12 at 20:13
1

When you a table you have to declare it's initial size: int table[SIZE];. When you don't know the table's size at compilation time (e.g. you read data from the user) you always can allocate memory in different way (size is a variable, not a constant):

int* table = (int*)malloc(sizeof(int)*size));

Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60
1

If you want to create a dynamic Table/array, you can do so using malloc function in C and using new operator in C++. You should deallocate the memory using free and delete depending on whether you use C or C++.

Recker
  • 1,915
  • 25
  • 55
1

You cannot dynamically define an array in that way. You must give it a storage size:

    int Table[43]; 

Another way would be to use malloc:

    int *Table = malloc (sizeof (int) * N);
    int i;

    for (i = 0; i < N; i++)
        Table[i] = i;

Where N would be passed by some means. Don't forget to use free() on the array after.

Andrew Corsini
  • 1,548
  • 3
  • 11
  • 11