指针
2015-12-26 16:12
225 查看
const和指针
int * const p; //指针常量
指针是常量,指针指向不可以改变,但指向的值可以改变
const int * const p; //常量指针常量
都不可以改变
数组指针与指针数组
int (*p)
数组指针
int *arr[3] 指针数组
多级指针:常用二级指针
函数与指针
指针函数
返回值* 函数名(形参){}; //返回值为指针
函数指针
函数返回值类型 (*fun)[形参表]
函数指针数组
类型 (*数组名【下标】)(参数)
int (*fun
)(int ,int)
指向指针函数的函数指针数组
int *(*fun
)(int ,int)
#define PI 3.14 不会进行类型检查 编译时
const float PI=3.14 编译时会进行类型检查
const float PI=3.14 编译时会进行类型检查
int const a;
const int a;
const int a;
const int *p; //常量指针
int const *p; //指针指向一个常量,指针指向可以改变
函数封装时用得比较多
FILE *fopen(const char *path, const char *mode);
void *memcpy(void *dest, const void *src, size_t n);
int const *p; //指针指向一个常量,指针指向可以改变
函数封装时用得比较多
FILE *fopen(const char *path, const char *mode);
void *memcpy(void *dest, const void *src, size_t n);
#include<stdio.h> int main() { int i=0; const int *p=&i; printf("%d\n",*p); //False *p=10; //error: assignment of read-only location ‘*p’ //True i=10; printf("%d\n",*p); }
#include<stdio.h> int main() { const int i=10; int *p=&i; // warning: initialization discards ‘const’ qualifier from pointer target type [enabled by def] *p=0; //i的值被改变 printf("%d\n",*p); }
int * const p; //指针常量
指针是常量,指针指向不可以改变,但指向的值可以改变
const int * const p; //常量指针常量
都不可以改变
数组指针与指针数组
int (*p)
数组指针
int *arr[3] 指针数组
#include<stdio.h> #include<stdlib.h> int main() { char *name[5]={"Follow me","Basic","Great","Fortran","Computer"}; int i; int j,k; char *temp; for(i=0;i<5-1;i++) { k=i; for(j=i+1;j<5;j++) { if(strcmp(name[k],name[j])>0) k=j; } if(k!=i) { temp=name[i]; name[i]=name[k]; name[k]=temp; } } for(i=0;i<5;i++) { puts(name[i]); } exit(0); }
多级指针:常用二级指针
函数与指针
指针函数
返回值* 函数名(形参){}; //返回值为指针
函数指针
函数返回值类型 (*fun)[形参表]
#include<Stdio.h> #include<stdlib.h> int add(int a,int b) { return a+b; } int main() { int a=3,b=5; int ret; int (*p)(int , int); int res; p=add; //add本身就是地址 ret=add(a,b); printf("%d\n",ret); res=p(a,b); printf("%d\n",res); exit(0); }
函数指针数组
类型 (*数组名【下标】)(参数)
int (*fun
)(int ,int)
指向指针函数的函数指针数组
int *(*fun
)(int ,int)
相关文章推荐
- quick cocos2d-x 下载地址
- 二叉树的定义与遍历
- 【C++】小项目――内存泄露检测器
- kafka系列教程3(设计构造及原理2)
- Linux Is Not Matrix——Nginx做成linux服务
- 20151226问题总结
- 【tyvj1463】智商问题
- 10个Android开源个性化View控件
- android随笔12——toast响应触摸事件以及移动
- Gradle 多渠道打包
- 批处理命令——call 和 start
- 初识Spring
- 2016年读书笔记
- spring配置durid连接池
- [精]Oracle APEX 5.0 新手教程(二) 表格式表单
- MIT 6.828 JOS学习笔记3. Exercise 1.2
- [C++] C++ 文件操作
- kafka系列教程2(设计构造及原理1)
- solr5.3+tomcat-7.0.65 solr从mysql数据库导入数据并创建索引
- Linux下Redis服务器安装配置教程