I'm trying to declare this buffer on global and when compiling displays the following error
error: initializer element is not constant
char *ls_buffer_PPouAUT = malloc(5120*sizeof(char));
How can I resolve it?
I'm trying to declare this buffer on global and when compiling displays the following error
error: initializer element is not constant
char *ls_buffer_PPouAUT = malloc(5120*sizeof(char));
How can I resolve it?
Placing the variable inside a function.
Do not use anything global. It is unnecessary in almost all cases. In cases that may be helpful you need to know what you are doing, understand all of the implications. You are probably using this inadvertently.
Of course, you can still declare the global variable and initialize it within the function, ideally right at the beginning of main()
not to be in invalid state. But avoid it, it's better. Create a local variable and go as an argument to other functions or store in a structure that makes sense.
Global variables are initialized during compilation. You can not execute the malloc()
function at that time, during compilation it can only have constant values, so they can be placed inside the generated executable.
Because this value can only be known at runtime, it must be at one point within the application's run flow. Only the functions have run flow.