为什么 c 数组声明为变量名而不是类型
Why is c-array declared at variable-name, instead of type
简介
在 c 中,声明给定类型 变量的语法如下所示:
// <variable-type> <variable-name>
// for example:
int foo;
要声明一个指针,您可以在类型后面或变量前面使用星号(*)名字.
int* bar;
int *foobar;
这个指针也可以不指向单个元素,而是指向给定数据类型.[=45]元素的数组 =]
例如字符数组(字符串):
char* oneString = "this is a string";
这样一个字符串的替代声明(编辑:虽然绝对不等同,正如评论中指出的那样)如下所示:
//<variable-type> <variable-name>[<count>]
//example:
char anotherString[1024] = "this is another string";
在 c# 中进行比较,你有相反的方式:
int[] intArray;
主要问题:
在变量名、而不是它的类型有效地声明数组的注意事项是什么?
作为上下文/进行比较
(不作为另一个问题,仅提供比较设计决策的方法)
人们创建 c#、打破此规则并将数组声明移至类型本身的原因可能是什么?
The alternate declaration of such a string then looks like the following
这是一个不同事物的声明。它不能替代第一个声明。在某些情况下,您可以互换使用它们,在其他情况下则不能。
In c# for comparison, you have it the other way around
不同的语言是不同的。
What were the considerations of effectively declaring an array at the variable-name, instead of its type?
设计考虑 "declaration follows use"。这意味着如果您使用这样的变量:a[i]
并且结果是 int
,您可以这样声明它:int a[N];
。如果你像这样使用它 *a
,你就声明了 int *a
。如果需要*a[i]
,声明int *a[N]
,如果需要(*a)[i]
,声明int (*a)[N]
。
What might have been the reasons for the people creating c#, to break with this rule
C# 的设计者认为 "declaration follows use" 原则令人困惑,在他们的语言中不需要。你来决定他们是否正确。
简介
在 c 中,声明给定类型 变量的语法如下所示:
// <variable-type> <variable-name>
// for example:
int foo;
要声明一个指针,您可以在类型后面或变量前面使用星号(*)名字.
int* bar;
int *foobar;
这个指针也可以不指向单个元素,而是指向给定数据类型.[=45]元素的数组 =] 例如字符数组(字符串):
char* oneString = "this is a string";
这样一个字符串的替代声明(编辑:虽然绝对不等同,正如评论中指出的那样)如下所示:
//<variable-type> <variable-name>[<count>]
//example:
char anotherString[1024] = "this is another string";
在 c# 中进行比较,你有相反的方式:
int[] intArray;
主要问题:
在变量名、而不是它的类型有效地声明数组的注意事项是什么?
作为上下文/进行比较
(不作为另一个问题,仅提供比较设计决策的方法)
人们创建 c#、打破此规则并将数组声明移至类型本身的原因可能是什么?
The alternate declaration of such a string then looks like the following
这是一个不同事物的声明。它不能替代第一个声明。在某些情况下,您可以互换使用它们,在其他情况下则不能。
In c# for comparison, you have it the other way around
不同的语言是不同的。
What were the considerations of effectively declaring an array at the variable-name, instead of its type?
设计考虑 "declaration follows use"。这意味着如果您使用这样的变量:a[i]
并且结果是 int
,您可以这样声明它:int a[N];
。如果你像这样使用它 *a
,你就声明了 int *a
。如果需要*a[i]
,声明int *a[N]
,如果需要(*a)[i]
,声明int (*a)[N]
。
What might have been the reasons for the people creating c#, to break with this rule
C# 的设计者认为 "declaration follows use" 原则令人困惑,在他们的语言中不需要。你来决定他们是否正确。