- 論壇徽章:
- 0
|
在 c專家編程書中48頁,有端程序/* 將源文件的timestamp轉(zhuǎn)換為表示當(dāng)?shù)馗袷饺掌诘淖址?/
char *localized_time(char *filename)
{
struct tm *tm_ptr;
struct stat stat_block;
char buffer[120];
/*獲得源文件的timestamp,格式為 time_t*/
stat(file,&stat_block);
/*把unix 的time_t轉(zhuǎn)換為tm結(jié)構(gòu),里邊保存當(dāng)?shù)貢r間*/
tm_ptr=localtime(&stat_block,st_time)
/*把tm結(jié)構(gòu)轉(zhuǎn)換成以當(dāng)?shù)貢r間*/
strftime(buffer,sizeof(buffer),"%a %b %e %T %Y",tm_ptr);
return buffer;
}
書上說return buffer這行錯誤, buffer是一個自動分配內(nèi)存的數(shù)組,是該函數(shù)的局部變量?刂屏麟x開聲明自動變量的范圍時,自動變量便自動失效。
但是我們調(diào)用一個函數(shù),都要先聲明的啊,那就意味著這個程序沒有問題啊!
比如一個簡單例子:
int add(int a,int b) {
int c;
c=a+b;
return c;
}
#include<stdio.h>
int add(int a,int b);
int main(void) {
int a,b,d;
a=1;
b=2;
d=add(a,b);
printf("%d\n",d);
}
這樣絕對可以得到返回值的。
大家把我分析下這個問題原因啊, |
|