tobycode tobycode - 1 month ago 6
C Question

Use return value of function as array size initialiser in C

I've written a function in C to get the size of the terminal:

int get_terminal_size()
{
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);

return w.ws_row * w.ws_col;
}


However, I want to use it as the size parameter when initialising an array in a struct:

struct level
{
[...]
char level_data[get_terminal_size()];
}


However, this doesn't compile, as the compiler tells me that the initialiser element needs to be constant (which makes sense, but assigning the return value to a
const int
doesn't work either).

Is there any other way of doing what I need to do in C?

Thanks in advance.

Answer Source

The size of the array has to be known at compilation time. If you want to allocate memory dynamically you have to use malloc.

char *level_data = malloc(get_terminal_size());
if (level_data == NULL)
{ /* handle allocation failure */ }
/* ...Do something with your array... */
free(level_data);

People in comments, thank you for the corrections.