测试代码:
#include<bits/stdc++.h> using namespace std; char * fun() { char s[] = "abcde"; printf("fun:%p ",s); return s; } char * fun1() { static char s[] = "abcde"; printf("fun1:%p ",s); return s; } char * fun2() { char *s = new char[6]; strcpy(s,"abcde"); printf("fun2:%p ",s); return s; } void fun4() { static int a = 0; a++; printf("%d ",a); } int main() { printf("%p %s ",fun(),fun()); printf("%p %s ",fun1(),fun1()); printf("%p %s ",fun2(),fun2()); fun4(); fun4(); fun4(); return 0; }
运行结果:
fun:0x7ffe5d532b00
fun:0x7ffe5d532b00
(nil) (null)
fun1:0x601060
fun1:0x601060
0x601060 abcde
fun2:0x2587030
fun2:0x2587050
0x2587050 abcde
1
2
3