在一个阵列指向获取其所有内容 [英] Pointing at an array to get all its contents

查看:144
本文介绍了在一个阵列指向获取其所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,阵列指向他们的第一个元素。

So, arrays are pointers to their first element.

浮色[4] = {rColor,gColor,bColor,阿尔法};

因此​​,只是普通的颜色&放大器;颜色[0] ;

Thus, just plain color points to &color[0];

现在假设我有一个正是如此结构:

Now suppose I have a struct thusly:

struct Colors{
float color[4];
};

现在我发现我能做到这一点相当精致:

Now I have found I can do this quite fine:

Colors myColor={{*color}};

我也可能只是这样做:

I could also just do this:

Colors myColor={{color[0]}};

我只是指着一个元素,但结构预计4,所以它一直在寻找过去的这个元素。

I am only pointing at one element, but the struct expects 4, so it keeps looking past this element.

首先,我要检查是细做,法律和好。显然,如果你正在传递一个大阵,这是语法和打字挺方便的。

First, I want to check that is fine to do, legal and okay. Obviously if you are passing a large array, this is quite convenient on the syntax and typing.

第二,我想验证为什么这个作品的理由。由于颜色孤单是一个指针, * 是一个间接运算符的检索指向的内存,因此,数组。所以基本上我们只需调用与间接指针得到整个数组。是否正确?

Second, I want to verify the reasoning about why this works. Since color alone is a pointer, the * is an indirection operator that retrieves the memory pointed to, thus, the array. So essentially we get the entire array by just calling its pointer with indirection. Correct?

推荐答案

的结构,联合Intializers和数组有,是根据它省略元素与 0

Intializers of structures, unions and arrays have the particular property that elements that are omitted from it are initialized with 0.

要初始化的整体结构,你的颜色您也可以使用相同类型的其他对象。

To initialize a whole structure as your Colors you can also use any other object of that same type.

这篇关于在一个阵列指向获取其所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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