我对 C/C++ 编译器的了解是,它们在初始化多维数组时会忽略内括号。
所以,你不能这样做:
int myArray[][] = { { 2, 3 }, { 4, 5 }, { 4, 1 } };
因为编译器会看到它完全一样
int myArray[][] = { 2, 3, 4, 5, 4, 1 };
现在它不知道是 6 * 1、3 * 2、2 * 3、1 * 6,还是别的什么(因为这可以是部分初始化列表,不一定完整)。
我的问题是,为什么这在许多编译器中都有效?
int myArray[][2] = { { 2 }, { 4, 5 }, { 4, 1 } };
编译器“直观地”将其视为:
int myArray[][2] = { { 2, 0 }, { 4, 5 }, { 4, 1 } };
这意味着它不会忽略大括号。到目前为止,我已经在三种不同的编译器上进行了尝试,并且都可以正常工作。
我希望答案是“这只是依赖于编译器”。我无权访问该标准,因此请提供来自该标准的答案。我不需要直觉,我有我的。
最佳答案
以下内容来自 K&R 的“The C Programming Language”,第 2 版,第 219,220 页的 A8.7 节:
An aggregate is a structure or array. If an aggregate contains members of aggregate type, the initialization rules apply recursively. Braces may be elided in the initialization as follows: if the initializer for an aggregate's member that is itself an aggregate begins with a left brace, then the succeeding comma-separated list of initializers initialize the members of the sub aggregate; it is erroneous for there to be more initializers than members. If, however, the initializer for a subaggregate does not begin with a left brace, then only enough elements from the list are taken to account of the members of the subaggregate; any remaining members are left to initialize the next member of the aggregate of which the subaggregate is a part. For example,
int x[] = { 1, 3, 5 };
声明并初始化 x 为具有三个成员的一维数组,因为没有指定大小并且
there are three initializers.
因此,鉴于这一行
int myArray[][2] = { { 2 }, { 4, 5 }, { 4, 1 } };
编译器将递归地初始化数组,注意每个子数组都以左大括号开始,并且初始化器的数量不超过所需数量,并将计算子数组的数量以确定数组的第一个维度。
以下内容来自 K&R 的“The C Programming Language”,第 2 版,第 220 页的 A8.7 节:
float y[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } };
is a completely-bracketed initialization:
1,3and5initialize the first row of the arrayy[0], namelyy[0][0],y[0][1], andy[0][2]. Likewise the next two lines initializey[1]andy[2]. The initializer ends early, and therefore the elements ofy[3]are initialized with0. Precisely the same effect could have been achieved by
float y[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
注意,在这两种情况下,数组的第四行都会被初始化 为零,因为没有指定足够的初始化器。
float y[4][3] = {
{ 1 }, { 2 }, { 3 }, { 4 }
};
初始化y的第一列,剩下的0。
所以编译器不会忽略内括号。但是,如果您按顺序指定所有初始值设定项且没有间隙,则内括号是可选的。如果您不想指定完整的初始化程序集,则使用内括号可以更好地控制初始化。
关于c++ - 初始化 C/C++ 多维数组时忽略大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22346426/
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat