#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_str1()
{
char *p = "abcdef1"; //文字常量区
return p;
}
char *get_str2()
{
char *q = "abcdef1"; //文字常量区
return q;
}
int main(void)
{
char *p = NULL;
char *q = NULL;
p = get_str1();
//%s: 指针指向内存区域的内容
//%d: 打印p本身的值
printf("p = %s, p = %d", p, p);
q = get_str2();
printf("q = %s, q = %d", q, q);
printf("\n");
system("pause");
return 0;
}
p指向“abcdef”这片常量区,例如0x11,指针指向谁就把谁的地址赋值给指针,p=get_str1();运行到这句话时,主函数p得到0x11的地址,主函数p指向0x11这片内存。q同样的道理。