当前位置:网站首页>Pointer to a two-dimensional array
Pointer to a two-dimensional array
2022-06-11 07:14:00 【lihongtao8209】
Recently, I used my mobile phone to learn C Language , I found a problem . Described below .
/* Whether the main test procedures can pass */
int main()
{
char test[5][5];
move(test);
}
void move(char** ptest)
{
;
}
Compiled with mobile phone software , The following information was obtained , It needs careful analysis .
A wrong passage
Compile error: …/Main.c: In function ‘main’:
…/Main.c:66:9: warning: passing argument 1 of ‘move’ from incompatible pointer type [-Wincompatible-pointer-types]
move(test);
^~~~
Wrong second paragraph
…/Main.c:27:19: note: expected ‘char **’ but argument is of type 'char (*)[5]
’
void move(char ** p)
~~~~~~~^
The first paragraph of the error explains the prerequisites for compilation errors :
gcc Compiler added
[-Wincompatible-pointer-types]
after ,
The compiler warns : Inconsistent pointer conversion
The second paragraph of the error tells the cause of the compilation error :
- Expectant (expected) Input type :char**
- move The entry parameter of is char (*)[5]
Pass in move The input parameters of the function char test[5][5], and move The type that the function accepts is Array pointer ( Short for pointer to a two-dimensional array ):char (*)[5]
Here are a few questions that need to be clear :
- At this point, the compiler , Think char test[5][5] Of “ Simplified type ” yes char (*)[5]
- Why does the compiler think that char ()[5], instead of char ()[6]、char (*)[7] Wait a minute ? Because the compiler based on the existing input parameters char test[5][5] Inferential .
So the correct way to write the above code is :
/* Whether the main test procedures can pass */
int main()
{
char test[5][5];
move(test);
}
void move(char (*ptest)[5])
{
;
}
Why? char test[5][5] Of “ Simplified type ” Not what the compiler thinks is wrong char** type , It is char (*)[5] type ?
Because this representation is a static representation of two-dimensional arrays . It can also be called row pointer notation . It can be defined as follows :
include<string.h>
#include<stdio.h>
char y[3][5];
typedef char (*line)[5] ;
int main(int argc, char *argv[])
{
line x=y;
strcpy(x[1],"54");
printf("%s",x[1]);
}
char** Can dynamically define a two-dimensional array . It can be defined as :
#include<stdio.h>
void test(char** p);
int main(int argc, char *argv[])
{
char **p = new char*[3];
for(int i = 0;i < 3;i++)
{
p[i] = new char[5];
}
test(p);
}
void test(char** p)
{
//test
p[0][2]=100;
p[0][1]=48;
p[2][4]=255;
for(int i=0;i<3;i++)
for(int j=0;j<5;j++)
{
printf("p[%d][%d]=%c\n",i,j,p[i][j]);
}
}
边栏推荐
- Biological sequence intelligent analysis platform blog (1)
- 品牌定位个性六种形态及结论的重大意义
- **Count the characters with the largest number of words**
- Interview question 17.08 Circus tower
- [deploy private warehouse based on harbor] 3 deploy harbor
- [并发进阶]——线程池总结
- 【CF#223 (Div. 2)】A. Sereja and Dima
- Atomicinteger atomic operation class
- 【CF #277.5 (Div. 2)】B. BerSU Ball
- 資深OpenStacker - 彭博、Vexxhost昇級為OpenInfra基金會黃金成員
猜你喜欢

JVM Learning record (7) - - class Loading Process and parent delegation Model

Object. Specific implementation and difference between create() and new

webserver

big.js--使用/实例

Create a form whose client area is 800 pixels by 600 pixels

教育专家王中泽老师多年经验分享:家庭教育不是附庸品

Shutter restraint container assembly

Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation

@JsonProperty注解

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员
随机推荐
Quality-aware Feature Aggregation Networkfor Robust RGBT Tracking
【CF#388 (Div. 2)】A. Bachgold Problem
Matplotlib,设置坐标刻度大小,字体/设置图例大小及字体/设置纵横坐标名称及字体及大小
Education expert Mr. wangzhongze: family education focuses on self growth
Janus feature草稿
河南高考VS天津高考(2008年-2021年)
Grayscale publishing through ingress
LEARNING TARGET-ORIENTED DUAL ATTENTION FOR ROBUST RGB-T TRACKING
First day of database
Biological sequence intelligent analysis platform blog (1)
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
The difference between TCP and UDP
Niuke wrong question 3.1
Leetcode-9.Palindrome Numbber
Phi and phi (Mobius inversion + formula)
Start the Nacos server of shell script
【LeetCode】-- 17. Letter combination of telephone number
@JsonProperty注解
Shell脚本之启动Nacos服务端
Shutter restraint container assembly