You are using static char buffer[BUF_LEN];
in your function. The keyword static
ensures that same space will be utilized in each function call. So when you sequentially set the values outside the function, they will end up pointing to the same space. Basically all indexes in array will be pointing to same string at the end.
To solve this, allocate new memory each time
char * generateStackFrame (char stackVals[]) {
char* buffer;
buffer = malloc(sizeof(char)*BUF_LEN);
///rest of code
return buffer;
}
Note- dont use stack variable since it will be lost after returning from function call, allocate memory on heap using malloc
char * generateStackFrame (char stackVals[]) {
char buffer[BUF_LEN]; //this is wrong