测试代码:
#include<bits/stdc++.h> using namespace std; char * fun() { char s[] = "abcde"; printf("fun:%p\n",s); return s; } char * fun1() { static char s[] = "abcde"; printf("fun1:%p\n",s); return s; } char * fun2() { char *s = new char[6]; strcpy(s,"abcde"); printf("fun2:%p\n",s); return s; } int main() { printf("%p %s\n",fun(),fun()); printf("%p %s\n",fun1(),fun1()); printf("%p %s\n",fun2(),fun2()); return 0; }
运行结果:
fun:0x7ffdba452540 fun:0x7ffdba452540 (nil) (null) fun1:0x601060 fun1:0x601060 0x601060 abcde fun2:0x1fd3030 fun2:0x1fd3050 0x1fd3050 abcde
时间: 2024-11-14 11:58:42