How do I fix this line to work with my code. I know in C99 variable-length arrays are allowed, but I need to use malloc to fix this line. How would I go about doing that. char stack[strlen(input)];
(I am not using C99, by the way)
CodePudding user response:
Declare stack
as pointer and use malloc
.
char *stack = malloc(strlen(input));
Don't forget to free
the memory.
CodePudding user response:
Allow me to add that it's recommended to always the value of a pointer you just allocated with malloc.
You can do it this way :
char *stack = malloc(strlen(input));
if(stack == NULL){
return -1;
}
You can modify the -1 with whatever you want depending where you are using this part of code. (-1 being what people usually returns when an error occurs)
Why do that ? malloc function can fail allocating memory space in case RAM is full for example (this doesn't happen often but it has to be catched for code correctness)