指针变量与其指向内存的关系
指针变量也是一种变量,占有内存空间,用来保存内存地址测试指针变量占有内存空间大小。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
int main0101()
{
char* p = NULL;
char buf[] = "abcde";
printf("p1=%d\n", p);
//改变指针变量的值
p = buf;
printf("p2=%d\n", p);
//指针变量和它指向的内存块是两个不同的概念
p = p + 1;//改变指针变量的值,即改变了指针的指向
printf("p3=%d\n", p);
printf("buf=%s\n", buf);
printf("*p=%c\n", *p);//b
printf(" 改变指针指向的内存,并不会改变指针的值\n");
buf[1] = '1';
printf("p4=%d\n", p);
printf("buf2=%s\n", buf);
*p = 'm';
printf("p5=%d\n", p);
printf("buf3=%s\n", buf);
//写内存时,一定要确保内存可写
//char* buf2 = "aaawwweee";//该字符串在文字常量区 不可修改
//buf2[2] = '1';//err
char buf3[] = "wwweerrr";
buf3[1] = 's';//ok
//不允许向NULL和未知非法地址拷贝内存。
char* p3 = NULL;//err
//char* p3 = 0x1111;//err
//给p3指向的内存中拷贝字符串
p3 = buf3;//ok
strcpy(p3, "123");
return 0;
}