首先先来对指针就行一个简单的回顾.
指针(指针是什么,指针和指针的类型)
在对指针有了一个简单的了解之后,接下来我们就来看看指针更深层次的内容.
在我们的一级指针中,其实它不光光能指向一个数字
int i = 10;
int* p = &i;
除此之外,还有字符指针,数组指针,函数指针等等.
首先我们来看字符指针,通过前面对指针的简单回顾,我们知道,在指针的类型中有一种指针类型为字符指针 char*.
字符指针的一般使用:
int main(){
char ch = 'w';
char* pc = &ch;
*pc = 'w';
system("pause");
return 0;
}
看一段代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
char* pstr = "hello world";
printf("%s\n", pstr);
system("pause");
return 0;
}
我们这样去做是不是就把字符串"hello world"放到pstr中去了呢?千万不要这么想!这里的本质是将字符串"hello world"的首地址放在了pstr中.
接下来看这样一道题目:
#include <stdio.h>
#include <stdlib.h>
int main(){
char str1[] = "hello world";
char str2[] = "hello world";
char* str3 = "hello world";
char* str4 = "hello world";
if (str1 == str2){
printf("str1 and str2 are same\n");
}
else {
printf("str2 and str2 are not same\n");
}
if (str3 == str4){
printf("str3 and str4 are same\n");
}
else {
printf("str3 and str4 are not same\n");
}
system("pause");
return 0;
}
结果如下:
这道题目也就验证我们之前所强调知识点.
str1 == str2 与 str3 == str4所判断的是首元素地址,str1与str2是分辨开辟出来的两块内存,首地址必然不相同,但str3与str4是两个指针,都保存了字符串"hello world"的首元素地址,都指向同一个字符创所以得到如上图所示的结果!
接下来回顾数组指针.
关于数组指针,指针数组,二级指针
对函数指针的回顾