当前位置:网站首页>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]);
}
}
边栏推荐
- Duality-Gated Mutual Condition Network for RGBT Tracking
- MS office level II wrong question record [7]
- P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
- 337. house raiding III
- Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
- RGB-D Salient Object Detection withCross-Modality Modulation and Selection
- Listen to the left width of the browser to calculate the distance
- QT 基于QScrollArea的界面嵌套移动
- mybaits-puls 在xml文件中写sql语句 会报错 Invalid bound statement (not found):
- Difference between byte and bit
猜你喜欢

【LeetCode】-- 17.电话号码的字母组合

Leetcode hot topic 100 topic 6-10 solution

Education expert wangzhongze solves students' problems with one move

Gobang interface of mobile console (C language)

Leetcode-104. Maximum Depth of Binary Tree
![[deploy private warehouse based on harbor] 4 push image to harbor](/img/af/8e28b229d94f3e6eab02308b69dc74.jpg)
[deploy private warehouse based on harbor] 4 push image to harbor

Decimal to binary

Create a form whose client area is 800 pixels by 600 pixels
![[Xunwei dry goods] opencv test of Godson 2k1000 development board](/img/94/312bb1f0d5e8d49506f659ad23cd3a.jpg)
[Xunwei dry goods] opencv test of Godson 2k1000 development board

CMAP of Matplotlib
随机推荐
Henan college entrance examination vs Tianjin college entrance examination (2008-2021)
农房一体脚本的心得记录
Leetcode-647.Palindromic Substrings
es5和es6的学习小记
Leetcode hot topic 100 topic 6-10 solution
河南高考VS天津高考(2008年-2021年)
Create a form whose client area is 800 pixels by 600 pixels
【CF #277.5 (Div. 2)】B. BerSU Ball
1190. invert the substring between each pair of parentheses
[Xunwei dry goods] opencv test of Godson 2k1000 development board
MS office level II wrong question record [8]
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
mybaits-puls 在xml文件中写sql语句 会报错 Invalid bound statement (not found):
Mistakes in Niuke JS exercise
通过 Ingress 进行灰度发布
[deploy private warehouse based on harbor] 4 push image to harbor
213. house raiding II
Group arrays by a specified size
Matplotlib, set coordinate scale size, font / set legend size and font / set vertical and horizontal coordinate name, font and size
Shuttle container component