所以我有以下toString函数:
/*
* 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;
}
CLion突出显示返回行并发出警告: Value转义本地范围(指代transactStr)
我需要知道为什么会这样(我是C,btw的新手)
答案 0 :(得分:17)
你已经在该函数中定义了一个局部变量指针(编辑感谢)并试图返回它。
这是一个禁忌,因为变量的生命周期只是它的封闭范围,这里是函数调用。如果你幸运的话,任何试图引用返回值的人都会触发未定义的行为,通常是崩溃。
如果要返回数组,则需要将其作为参数传递。