33,311
社区成员
发帖
与我相关
我的任务
分享
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char * getStr()
{
char *p = "hi,friend";
return p;
}
int * getInt()
{
int a = 12;
int *p = &a;
printf("&a=%p, &p=%p\n", &p, p);
return p;
}
void main()
{
int *c = NULL;
c = getInt();
printf("c:%p %d\n", c,*c);
printf("hello...\n");
system("pause");
return;
}
char * getStr()
{
char *p = "hi,friend";
return p;
}
接收getStr函数返回的地址是可以的,原因的是"hi,friend"的地址不在栈上,而是在只读数据段。因此返回的是只读数据段的地址,因此是可以正常输出字符串的。#include "stdafx.h"
#include <typeinfo>
#include <iostream>
#include <stdio.h>
using namespace std ;
int* fun1()
{
int c=100;
return &c;
}
int* fun2()
{
int d=144;
return &d;
}
int _tmain(int argc, _TCHAR* argv[])
{
int* c= fun1();
int* d= fun2();
printf("%d,%p",*c,c);
return 0;
}
最后输出多少呢?
这段代码可能会加深你的认识!