C: Does the value go beyond the local area?

So, I have the following toString function:

/* * Function: toString * Description: traduces transaction to a readable format * Returns: string representing transaction */ char* toString(Transaction* transaction){ char transactStr[70]; char id[10]; itoa(transaction -> idTransaction,id, 10); strcat(transactStr, id); strcat(transactStr, "\t"); char date[15]; strftime(date,14,"%d/%m/%Y %H:%M:%S",transaction -> date); strcat(transactStr, date); strcat(transactStr, "\t"); char amount[10]; sprintf(amount,"%g",transaction -> amount); strcat(transactStr,"$ "); strcat(transactStr, amount); return transactStr; } 

I think this is a CLION problem that highlights the return line with a warning: The value is outside the local scope (referring to transactStr)

I need to know why this is happening (I'm new to C, btw)

+5
source share
1 answer

Inside this function, you specified a local variable pointer (edit thanks) and try to return it.

This is no-no, because the lifetime of a variable is just the value that spans the area, here is the function call. Anyone who tries to reference the return value causes undefined behavior, usually crashing if you're lucky.

If you want to return an array, you need to pass it as an argument.

+11
source

Source: https://habr.com/ru/post/1268286/


All Articles