当前位置:网站首页>c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
c语言语法基础之——指针( 多维数组、函数、总结 ) 学习
2022-06-26 09:33:00 【坦桑尼亚奥杜威峡谷能人】
普通指针:
二维数组,一般是几行几列;
如多定义一个普通指针的话,此指针只代表第几行的地址,而不是某个元素地址,比如:
int a[3][3]={
1,2,3,4,5,6,7,8,9};
int *p;
p=a; //
printf("%d\n",*p); // 1 第一行第一列的元素值
p=a+1;
printf("%d\n",*p); // 4 第二行第一列的元素值
可以定义一个指定地址的指针:
int a[3][3]={
1,2,3,4,5,6,7,8,9};
int *p;
p=&a[1][1]; // 第二行第二列元素的地址
printf("%d\n",*p); // 5 第一行第一列的元素值
p++;
printf("%d\n",*p); // 6 第二行第三列的元素值
p=a+1; // 第二行
printf("%d\n",*p); // 4 第二行第一列的元素值


int main(int argc, char *argv[]) {
/* 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 */
int a[5][5];
int i,j;
for(i=0;i<5;i++){
for(j=0;j<5;j++){
// a[i][j]=i-j+1; // 非指针版
*(a[i]+j)=i-j+1; // 指针版
*(*(a+i)+j)=i-j+1; // 指针版
if(i>=j){
// printf(" %d", a[i][j]); // 非指针版
// printf(" %d", *(a[i]+j)); // 指针版
printf(" %d", *(*(a+i)+j)); // 指针版
}
}
printf("\n");
}
return 0;
}
数组指针:
定义行指针(数组指针):就可以使用*(*(p+i)+j,否则报错,只能用*(*(a+i)+j
int a[3][3]={
1,2,3,4,5,6,7,8,9};
int (*p)[3]; // 数组的行指针


数组的几种指针运算:

注意图中的是逗号,执行顺序是从右到左的,如果是逗号改成乘号,是从左到右的
int a[]={
1,2,3},y,*p=&a[2];
y=*--p;
y的值: 2
逻辑:p刚开始指向下标为2的地址,先--,移动到下标为1,然后*取值,下表为1的地址处,值是2
int a[]={
1,2,3,4,5,6},*p=a;
*p++ * *++p 的值是:3
逻辑:乘号,是从左往右执行,先*p=1,然后指针加1,在执行++p,指针在加1,然后再取*++p=3 , 最后 1*3=3
函数指针:
int max(int a, int b){
}
int (*p)()
max(a,b) --> (*p)(a,b)
指针数组:
定义:
每一个下标变量都是一个指针变量,是一个地址,主要用于处理变长多字符串。
理解:指针数组和之前的数组指针完全不是一个概念,指针数组特殊在所有元素都是一个地址
样式:
int *b[3]={ a,&a[1] }
指针总结:

// 圆的面积
double area(int r){
return 3.14*r*r;
}
int main(int argc, char *argv[]) {
double (*p)(); // 注意类型必须要和函数类型一致,如果函数是double,这里是int,结果会变成 4199728.00000
p=area;
int i=2;
double result;
result=(*p)(i);
printf("%f",result); // 12.560000
return 0;
}
边栏推荐
- How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card
- 工企专利匹配数据(数十万数据量)1998-2014年
- Catalogue gradué de revues scientifiques et technologiques de haute qualité dans le domaine de l'informatique
- Cancellation and unbinding of qiniu cloud account
- 同花顺炒股软件安全性怎样?在同花顺怎么开户
- Collection object replication
- 全面解读!Golang中泛型的使用
- LeetCode 剑指 Offer II 091.粉刷房子 - 原地修改
- "One week's work on Analog Electronics" - power amplifier
- Merrill Lynch data technology expert team | application of recommendation of relevant contents in group system data retrieval
猜你喜欢

Thinkphp5 manual error reporting

【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id

Champions League data set (Messi doesn't cry - leaving Barcelona may reach another peak)

Master data management of scientific research institutes? Suppliers or customers? I am a correspondent

"One week's work on digital power" -- encoder and decoder

How to create an IE tab in edge browser

【CVPR 2021】DatasetGAN: Efficient Labeled Data Factory with Minimal Human Effort

"One week's work on Analog Electronics" - Basic amplification circuit

Yolov5 results Txt visualization

正则表达的学习
随机推荐
进入页面输入框自动获取焦点
"One week to finish the model electricity" - 55 timer
PHP extracts TXT text to store the domain name in JSON data
Use recursion or a while loop to get the name of the parent / child hierarchy
"One week's work on digital power" -- encoder and decoder
【Sensors 2021】Relation-Based Deep Attention Network with Hybrid Memory for One-Shot Person Re-Id
install opencv-contrib-dev to use aruco code
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead. npm ER
Cancellation and unbinding of qiniu cloud account
LeetCode 498. 对角线遍历
How to solve the problem that NVIDIA model cannot be viewed by inputting NVIDIA SMI and quickly view NVIDIA model information of computer graphics card
The 100000 line transaction lock has opened your eyes.
mysql 数据库字段查询区分大小写设置
install ompl.sh
Champions League data set (Messi doesn't cry - leaving Barcelona may reach another peak)
Several connection query methods of SQL (internal connection, external connection, full connection and joint query)
Thinking before QPM preparation optimization
Differences between VI and vim and common commands
Optimization of power assisted performance of QPM suspended window
Is it safe to dig up money and make new debts