是什么在C变量声明括号是什么意思? [英] What do parentheses in a C variable declaration mean?

查看:130
本文介绍了是什么在C变量声明括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释这是什么意思?

  INT(*数据[2])[2];


解决方案

什么是括号?

在C语言中括号[]有较高的precedence比星号*

维基百科不可解释:


  

要声明一个变量作为一个
  指向数组的指针,我们必须利用
  圆括号。这是因为在C
  括号([])有较高的precedence
  比星号(*)。所以,如果我们希望一个指针声明为数组,我们需要提供括号来覆盖这一点:


 双(*大象)[20];


  

这声明大象是一个
  指针,它指向的类型是
  数组20 double值。


  
  

要声明一个指针数组
  指针,简单地结合
  符号。


 为int *(*鳄鱼)[15];

来源

您的实际情况:

  INT(*数据[2])[5];

数据是2个元素的阵列。每个元素都包含一个指向5 int数组。

所以,你可以使用数据类型有code:

  INT(*数据[2])[5];
INT X1 [5];
数据[0] =&放大器; X1;
数据[1] =&放大器; X1;数据[2] =&放大器; X1; //< ---出界,碰撞数据没有第三个元素
INT Y1 [10];
数据[0] =&放大器; Y1; //< ---编译错误,数据的每个元素必须指向一个int [5]没有一个int [10]

Can someone explain what this means?

int (*data[2])[2];

解决方案

What are the parentheses for?

In C brackets [] have a higher precedence than the asterisk *

Good explanation from Wikipedia:

To declare a variable as being a pointer to an array, we must make use of parentheses. This is because in C brackets ([]) have higher precedence than the asterisk (*). So if we wish to declare a pointer to an array, we need to supply parentheses to override this:

double (*elephant)[20];

This declares that elephant is a pointer, and the type it points at is an array of 20 double values.

To declare a pointer to an array of pointers, simply combine the notations.

int *(*crocodile)[15];

Source.

And your actual case:

int (*data[2])[5];

data is an array of 2 elements. Each element contains a pointer to an array of 5 ints.

So you you could have in code using your 'data' type:

int (*data[2])[5];
int x1[5];
data[0] = &x1;
data[1] = &x1;

data[2] = &x1;//<--- out of bounds, crash data has no 3rd element
int y1[10];
data[0] = &y1;//<--- compiling error, each element of data must point to an int[5] not an int[10]

这篇关于是什么在C变量声明括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆