空指针 野指针 万能指针
int *p = NULL;
int a =1000;
p = &a;
void* xx = p;
*(int*)p = 1000000;
printf("%d \n",*p);
const 修饰指针
const int a = 10;
int b = 100;
//第一种
int *p = &a;
p = &b; (V)
*p = 1000; (V)
//第二种
const int *p = &a;
p = &b; (V)
*p = 1000; (X)
//第三种
int * const p = &a;
p = &b; (X)
*p = 1000; (V)
//第四种
const int * const p = &a;
p = &b; (X)
*p = 1000; (X)
指针数组
char* arr[] = {"aaaa","bbbb","cccc"};
arr[0]表示"aaaa"的地址值
*(arr[0]+1) 表示“aaaa”中第二个元素的值
[]的优先级高 ,是的char* arr[]的本质是 一个数组,数组的元素为指针类型
数组指针
char hello[] = {"hello world"};
char (*pHello)[] = &hello;
pHello是一个指针类型,指向的元素为char[]
指针做参数和返回值
当指针或者数据做为实参时,都被按照指针进行处理。
如果是非字符指针或者所有的数组,一定要传递其长度进入;如果是字符串指针,可以通过strlen判断或者‘\0’的标志判断。
返回值
char* test(){
//Address of stack memory associated with local variable 'hello' returned
// 字符数组,创建在栈区
char hello[] = "hello world";
// 字符串常量 常量区
// char *hello = "hello world";
return hello;
}
int main(int argc, const char * argv[]) {
char * p = test();
printf("%p \n",p);
printf("%s \n",p);
return 0;
}
来源:CSDN
作者:C安君
链接:https://blog.csdn.net/dirksmaller/article/details/103788850