传递结构数组功能的C ++ [英] Passing array of structures to function c++

查看:103
本文介绍了传递结构数组功能的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,小白的问题,我只是有点困惑。结果
如果我在主结构数组,我想传递给一个函数:

Sorry for the noob question I'm just a little confused.
If I have an array of structures in main that I want to pass to a function:

struct MyStruct{
    int a;
    int b;
    char c;
    mayarray[5];
};  
MyStruct StructArray[10]; 

myFunction(StructArray[])

传递给这一个功能:

Pass to this to a function:

void myFunction(struct MyStruct PassedStruct[])
{
    PassedStruct[0].a = 1;
    PassedStruct[0].b = 2;
    // ... etc
}  

我的问题是,将调用这样的功能修改 StructArray 的数据?我需要它。请问这是否通过引用调用?我有点困惑。我将如何改变它,这样,当我的结构数组传递给函数,该函数将修改数组 StructArray ?顺便说一句I'musing视觉工作室。结果
谢谢你。

My question is, will calling the function like this modify the data in StructArray? I need it to. Would that be call by reference? I'm a little confused. How would I change it so that when I pass the array of structures to the function, the function will modify the array StructArray? I'musing visual studio btw.
Thanks.

推荐答案

结构MYSTRUCT PassedStruct [] 主要是为结构MYSTRUCT替代语法* PassedStruct 。所以,是的,你会访问并修改原来的结构。

struct MyStruct PassedStruct[] is mostly an alternative syntax for struct MyStruct * PassedStruct. So yes, you will access and modify the original structure.

只是一个细节变化,正确调用该函数是不是 myFunction的(StructArray []); ,但 myFunction的(StructArray);

Just one detail to change, the correct call to to the function is not myFunction(StructArray[]); but myFunction(StructArray);

现在我将试图解释为什么我在上面的句子中使用上面的字大多。而这是不是很好。

Now I will try to explain why I used the above word mostly in the above sentence. And that is not nice.

我会尽量给一些暗示的差异阵列之间的指针,为什么你不应该混淆他们(即使我不会说他们是不相关的,恰恰相反),什么是与上面的<$ c中的问题$ C> MYSTRUCT PassedStruct [] 参数传递的语法。

I will try to give some hint on difference between arrays an pointers, why you shouldn't confuse them (even if I would not say they are unrelated, quite the opposite), and what's the problem with the above MyStruct PassedStruct[] parameter passing syntax.

这可能是不适合初学者和害羞的人,和C ++标准专家通常还应该避免读取(而不是出于同样的原因,而是因为我不想在某些获得 - ISO Standard_战争,因为我进入ISO 未定义行为的领土 - 你可以理解禁地,如果你preFER)

This is probably not for beginners and shy people, and C++ standard expert should probably also avoid reading that (not for the same reason, but because I don't want to get in some - ISO Standard_ war as I enter ISO undefined behavior territory - you may understand forbidden territory if you prefer).

这是因为事情是一个挑战 - 复杂的是不是真正的一句话 - 我想说的扭曲。

It's a challenge as things are - complicated is not really the word - I would say twisted.

让我们从开始的数组:

试想一个简单的结构:

struct MyStruct{
    int a;
    int b;
    char c;
};  

MYSTRUCT A1 [3]; 是一个数组,其项目是上述结构类型的声明。 ,做编译器,当你定义一个数组中最重要的是为它分配空间。在我们为例它预留一些空间3结构。该预留空间可以在堆栈或从那里被放在声明语句根据全局内存资源。

MyStruct a1[3]; is the declaration of an array whose items are of the above structure type. The most important thing that does the compiler when you define an array is allocate space for it. In our exemple it reserved some space for 3 structs. This reserved space can be on stack or from global memory resources depending where is put the declaration statement.

您也可以像在结构MYSTRUCT A1 [3] = {{1,2},{3,4},{5,6}}声明时初始化结构C $ C>的通知不是我没有在这个例子中初始化C字段,只是a和b。允许我,因为我想做到这一点。我只能用designater语法,如果我的编译器支持,它就像在结构MYSTRUCT A1 [3] = {{A:1,B:2},{一:3,B:4},{A: 5,b:6}};

现在,还有另一种语法用于使用空正方形backets像限定在一个数组结构MYSTRUCT A2 [] = {{1,2},{3,4},{5,6}} ; 。在这里赶的一点是,A2是一个完全正常的阵列正是因为A1。数组大小没有隐式的,它是通过初始化给出。在这里,我有三个初始化,因此我得到三个结构数组。如果我想我甚至可以用这样的语法定义已知大小的未初始化数组。如果我想大小3的未初始化数组我只是写结构MYSTRUCT A2 [] = {{} {} {}}; 。空间分配,完全一样的previous语法,没有这里所涉及的指针。

Now, there is another syntax for defining an array using empty square backets like in struct MyStruct a2[] = {{1, 2}, {3, 4}, {5, 6}};. The point to catch here is that a2 is a perfectly normal array exactly as a1. The array size is not implicit, it is given through the initializer. Here I has three initializers hence I get an array of three structs. I could even define an unitialized array of known size with this syntax If I want. If I want an uninitialized array of size 3 I just write struct MyStruct a2[] = {{},{},{}};. Space is allocated, exactly as with the previous syntax, no pointer involved here.

让我们介绍一款指针 MYSTRUCT * P1; 这是一个简单的指向类型MYSTRUCT的结构。我可以通过通常的指针语法访问域 P1-&gt;将(* P1)。A 。还有一个数组味语法做同样如上 P1 [0] .A 总是与上面相同。你只需要记住,P1 [0]是的简写(*(P1 + 0))。还记得规则指针运算:加1的指针指添加的sizeof尖锐的物体底层存储器地址(当你使用%P printf格式参数,你会得到什么)。指针运算允许访问其他连续张玉峰结构。这意味着你可以进入到P1 [0],P1 [2]等边界不检查。什么是指向的内存是程序员的责任。是的,我知道ISO说不同,那正是我曾经尝试过所有的编译器做的,如果你知道一个不的,请告诉我。为了使有用的东西与P1,你必须使它指向型MYSTRUCT的一些结构。如果你有这样的结构可用数组像我们的A1,你可以做 P1 = A1 和P1将指向数组的开头。换句话说,你也可以做 P1 =&放大器; A1 [0] 。是很自然的有一个简单的语法可用,因为它正是指针运算是专为:同一对象的访问数组

Let's introduce one pointer MyStruct * p1; This is a simple pointer to a structure of type MyStruct. I can access fields through usual pointer syntax p1->a or (*p1).a. There is also an array flavored syntax to do the same as above p1[0].a always the same as above. You just have to remember that p1[0] is a shorthand for (*(p1+0)). Also remember rule for pointer arithmetic : adding 1 to a pointer means adding the sizeof the pointed object to underlying memory address (what you get when you use %p printf format parameter). Pointer arithmetic allow accessing to other successive indentical structs. That means you can access to p1[0], p1[2], etc. Boundaries are not checked. What is in memory pointed to is programmer responsibility. Yes, I know ISO says differently, that is just what all compilers I ever tried do, if you know of one that does not, please tell me. To make anything useful with p1, you have to make it point to some struct of type MyStruct. If you have an array of such structs available like our a1, you can just do p1=a1 and p1 will point to the beginning of the array. In other words you could also have done p1=&a1[0]. It's natural to have a simple syntax available as it's exactly what pointer arithmetic is designed for : accessing arrays of identical objects.

该公约的妙处在于,它可以让完全统一指针和数组访问语法。所不同的是只看到由编译器:
- 当它看到 P1 [0] ,它知道它必须取的名字是一个变量的内容 P1
并且,它将含有一些存储器结构的地址。
- 当它看到 A1 [0] ,它知道 A1 一些常量,应被理解为一个地址(不东西在内存中获取)。

The beauty of that convention is that it allows to completely unify pointer and array access syntax. The difference is only seen by the compiler: - when it sees p1[0], it knows it has to fetch the content of a variable whose name is p1 and that it will contain the address of some memory structure. - when it sees a1[0], it knows a1 is some constant that should be understood as an address ( not something to fetch in memory).

但是,一旦从地址 P1 A1 可处理是相同的。

But once the address from p1 or a1 is available the treatment is identical.

一个常见的​​错误是写 P1 =&放大器; A1 。如果这样做,编译器会给你一些四个字母的单词。好吧,&放大器; A1 也是一个指针,但服用 A1 的地址时,你得到的是一个指针整个阵列。这意味着,如果你加1,这种类型的指针的实际地址将3层结构的步骤,一次移动。那种指针的实际类型(姑且称之为 P2 )将 MYSTRUCT(* P2)[3]; 。现在,你可以写 P2 =安培; A1 。如果你想在内存块的开始访问的第一个结构MYSTRUCT指向P2你将不得不写东西像 P2 [0] [0] .A (* P2)[0] .A (*(* P2))。在(* P2) - &gt;将 P2 [0] - &gt;将。由于类型系统和指针运算所有这些都在做同样的事情。取包含在P2地址,使用该地址作为数组(已知常量地址)如上所述。

A common mistake is to write p1 = &a1. If you do so the compiler will give you some four letter words. Ok, &a1 is also a pointer, but what you get when taking the address of a1 is a pointer to the whole array. That means that if you add 1 to a pointer of this type the actual address will move by steps of 3 structures at once. The actual type of a pointer of that kind (let's call it p2) would be MyStruct (*p2)[3];. Now you can write p2 = &a1. If you want to access to the first struct MyStruct at the beginning of the memory block pointed to by p2 you will have to write things like p2[0][0].a or (*p2)[0].a or (*(*p2)).a or (*p2)->a or p2[0]->a. Thanks to type system and pointer arithmetic all of these are doing exactly the same thing. Fetch address contained in p2, use that address as an array (a known constant address) as explained above.

现在,你可以理解为什么指针和数组的完全不同类型的不应该混淆的一些可能会说。用通俗的话指针包含地址变量,数组常量地址。请不要拍我的C ++大师,是的,我知道这是不是完整的故事和编译器保持许多其它信息与地址,尖为例(解决?)物体的大小一起。

Now you can understand why pointers and arrays are totally different types that should not be confused as some may say. In plain words pointers are variable that contains an address, arrays are constant addresses. Please don't shoot me C++ Gurus, yes I know that is not the full story and that compilers keep many other informations along with address, size of pointed (addressed ?) object for exemple.

现在,你可以不知道为什么在参数传递情况下,您可以使用空方括号和它的真正含义的指针的。 ?不知道。有人可能认为它看起来不错。

Now you could wonder why in parameter passing context you can use empty square brackets and it really means pointer. ? No idea. Someone probably thought it looked good.

顺便说一句,至少与海湾合作委员会,你可以也把括号之间的一些值,而不是使它们保持空。它不会有所作为你仍然会得到一个指针,而不是一个数组,边界或类型检查都没有做。在ISO标准Din't检查是应该做的,它它是由标准所要求的是一个具体的行为。

By the way, at least with gcc, you can also put some value between brackets instead of keeping them empty. It won't make a difference you'll still get a pointer, not an array, and boundaries or type checking are not done. Din't checked in ISO standard was should be done and it it's required by standard of is a specific behavior.

如果您想输入检查边界只使用一个参考。这可能是令人惊讶的,但是这就是如果你用一个参考,参数的实际类型指针变为阵列(而不是从指针引用作为可以预期指针)的区域。

If you want type checking for boundaries just use a reference. That may be surprising but this is an area where if you use a reference, the actual type of the parameter is changed from pointer to array (and not from pointer to reference to pointer as may be expected).

MyStruct StructArray[10]; 


  • 标题:无效myFunction的(结构MYSTRUCT * PassedStruct)

  • 来电者: myFunction的(StructArray)

  • 状态:工作,你在PassedStruct指针
  • 工作


    • 标题:无效myFunction的(结构MYSTRUCT PassedStruct [])

    • 来电者: myFunction的(StructArray)

    • 状态:工作,你在PassedStruct指针
    • 工作

    • 标题:无效myFunction的(结构MYSTRUCT(安培; PassedStruct)[10])

    • 来电者: myFunction的(StructArray)

    • 状态:工作,你要的大小10
    • 的数组的引用工作
    • header: void myFunction(struct MyStruct (& PassedStruct)[10])
    • caller: myFunction(StructArray)
    • status: works, you work with a reference to an array of size 10

    • 标题:无效myFunction的(结构MYSTRUCT(安培; PassedStruct)[11])

    • 来电者: myFunction的(StructArray)

    • 状态:不能编译,式原型和实际prameter阵列之间的不匹配


    • 标题:无效myFunction的(结构MYSTRUCT PassedStruct [10])

    • 来电者: myFunction的(StructArray)

    • 状态:作品,PassedStruct是一个指针,只要大小是忽略


    • 标题:无效myFunction的(结构MYSTRUCT PassedStruct [11])

    • 来电者: myFunction的(StructArray)

    • 状态:作品,PassedStruct是一个指针,只要大小是忽略

    这篇关于传递结构数组功能的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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