user-0 提问: 调用字符串型子函数返回值开头有乱码,求解决

回复:
user: user-1
content: #include<time.h> #include<stdio.h> char *timerec() {     static char tt[15];     time_t rawtime;     struct tm *timeinfo;      time(&rawtime);     timeinfo=localtime(&rawtime);     sprintf(tt,   "%4d",   1900+timeinfo->tm_year);     sprintf(tt,"%s%02d",tt,   1+timeinfo->tm_mon );     sprintf(tt,"%s%02d",tt,     timeinfo->tm_mday);     sprintf(tt,"%s%02d",tt,     timeinfo->tm_hour);     sprintf(tt,"%s%02d",tt,     timeinfo->tm_min );     return tt; } int main() {     char *s;     s=timerec();     printf("%s\n",s);     return 0; }

user: user-1
content: #include<time.h> #include<stdio.h> #include<string.h> char *timerec() {     static char tt[15];     time_t rawtime;     struct tm *timeinfo;      time(&rawtime);     timeinfo=localtime(&rawtime);     sprintf(tt,   "%4d",   1900+timeinfo->tm_year);     sprintf(tt,"%s%02d",tt,   1+timeinfo->tm_mon );     sprintf(tt,"%s%02d",tt,     timeinfo->tm_mday);     sprintf(tt,"%s%02d",tt,     timeinfo->tm_hour);     sprintf(tt,"%s%02d",tt,     timeinfo->tm_min );     return tt; } int main() {     char *s;     s=timerec();     printf("%s\n",s);     return 0; }

user: user-2
content: 传二级指针和返回指针并无实质区别。如果要解决问题，按楼上的，用 static 限定，或者内部分配内存，外部释放。

user: user-3
content: 额，其实这个题不需要传人二级指针，一个指针就可以的……

user: user-3
content: //返回堆指针 #include<memory.h> #include<stdlib.h> #include<time.h> #include<stdio.h> #include<string.h>  char *timerec() { 	char y[5],mon[3],d[3],h[3],mins[3],tt[15]; 	char *temp = (char* ) malloc(sizeof(char) * 15); 	 	time_t rawtime; 	struct tm *timeinfo; 	time(&rawtime); 	timeinfo=localtime(&rawtime); 	sprintf(y,"%4d",1900+timeinfo->tm_year); 	sprintf(mon,"%02d",1+timeinfo->tm_mon); 	sprintf(d,"%02d",timeinfo->tm_mday); 	sprintf(h,"%02d",timeinfo->tm_hour); 	sprintf(mins,"%02d",timeinfo->tm_min); 	strcpy(tt,y); 	strcat(tt,mon); 	strcat(tt,d); 	strcat(tt,h); 	strcat(tt,mins); 	strcat(tt,"\0"); 	memcpy(temp, tt,15); 	//temp=tt;//到这里为止用printf检查还都是正确的 	return temp; }  main() {     char *s;     s=timerec();//传下来就是乱码     printf("%s\n",s);     free(s);     s = NULL;     getchar(); }  //传入二级指针 #include<memory.h> #include<stdlib.h> #include<time.h> #include<stdio.h> #include<string.h>  void timerec(char** nowtime) { 	char y[5],mon[3],d[3],h[3],mins[3],tt[15]; 	//char *temp = new char[15]; 	time_t rawtime; 	struct tm *timeinfo; 	time(&rawtime); 	timeinfo=localtime(&rawtime); 	sprintf(y,"%4d",1900+timeinfo->tm_year); 	sprintf(mon,"%02d",1+timeinfo->tm_mon); 	sprintf(d,"%02d",timeinfo->tm_mday); 	sprintf(h,"%02d",timeinfo->tm_hour); 	sprintf(mins,"%02d",timeinfo->tm_min); 	strcpy(tt,y); 	strcat(tt,mon); 	strcat(tt,d); 	strcat(tt,h); 	strcat(tt,mins); 	strcat(tt,"\0"); 	memcpy(*nowtime, tt,15); 	//temp=tt;//到这里为止用printf检查还都是正确的 	//return temp; }  main() {     char *s = (char*)malloc(sizeof(char) * 15);     //s=timerec();//传下来就是乱码     timerec(&s);     printf("%s\n",s);  	free(s);      s = NULL;     getchar(); }

user: user-0
content: 我现在改成这样,干脆输出<null>了 void timerec(char **s) { char y[5],mon[3],d[3],h[3],mins[3],tt[15]; char *pt; time_t rawtime; struct tm *timeinfo; time(&rawtime); timeinfo=localtime(&rawtime); sprintf(y,"%4d",1900+timeinfo->tm_year); sprintf(mon,"%02d",1+timeinfo->tm_mon); sprintf(d,"%02d",timeinfo->tm_mday); sprintf(h,"%02d",timeinfo->tm_hour); sprintf(mins,"%02d",timeinfo->tm_min); strcpy(tt,y); strcat(tt,mon); strcat(tt,d); strcat(tt,h); strcat(tt,mins); pt=tt;printf("%s",pt); s=&pt;printf("%s",*s); }  main() {     char *s;     timerec(&s);     printf("%s",*s); }

user: user-0
content: 看不懂呢,2级指针返回的也出错了

user: user-3
content: 引用 2 楼 bastamon 的回复:传入2j指针前面看懂了,但是销毁是什么意思 额，你可以在函数里new出一个指针，并且返回这个指针……完成后就记得销毁你new出来的这个指针……new/delete  malloc/free

user: user-0
content: 传入2j指针前面看懂了,但是销毁是什么意思

user: user-3
content: 不要试图去返回局部变量的指针，因为跳出函数，这个栈也没有了，所以返回指针肯定是有问题的，你可以传入指针的指针也就是所谓的二级指针，或者返回new出来的指针，记得销毁……