简体   繁体   English

将指针传递给结构数组

[英]Passing Pointer to an Array of structures

This is for MPLABXC8 compiler I have researched and found number of topics related. 这是针对MPLABXC8编译器我研究并发现的相关主题数量。 But not able to solve my issue. 但无法解决我的问题。 My Array typedef 我的数组typedef

     typedef volatile struct OneStageOpTag
   {
        unsigned DevID1: 4;
        unsigned SetCmd1 : 4;
        unsigned RdyResponse1 :4;
        unsigned DevID2: 4;
        unsigned SetCmd2 : 4;
        unsigned RdyResponse2 :4;
        unsigned DevID3: 4;
        unsigned SetCmd3 : 4;
        unsigned RdyResponse3 :4;
    }OneStageOpType[3];

Now my variable 现在是我的变量

    OneStageOpType CurOperPlan={0};// I checked this one - 
        //-in Simulator 3 element array of structure created

Now I am passing pointer to my function 现在我将指针传递给我的函数

GetOperationSeqForTransportReq(1,1,&CurOperPlan);

below is the function 以下是功能

void GetOperationSeqForTransportReq(StationIDType SourseStnID,StationIDType DestiStnID,
                    OneStageOpType  *CurTransportPlan)
{
    NOP();
    CurTransportPlan[0]->DevID1=5;  // This is Ok
    CurTransportPlan[1]->DevID1=5;  // This is Not working      
}

only the 0th element is accessable. 只有第0个元素是可访问的。 Also compiler complaints that structure pointer passed to structure array pointer. 另外编译器抱怨结构指针传递给结构数组指针。 I tried by incrimenting the pointer in function. 我尝试通过在函数中执行指针。 It seems incrimenting the whole Array pointer. 它似乎正在推动整个数组指针。 It seems to me that &CurOperPlan is adress pointer to 0th index structure only. 在我看来, &CurOperPlan只是指向第0个索引结构的地址指针。 The whole array is not contained it. 整个数组不包含它。 please help. 请帮忙。

Change this 改变这个

GetOperationSeqForTransportReq(1,1,&CurOperPlan);

to be 成为

GetOperationSeqForTransportReq(1, 1, CurOperPlan);

and this 和这个

void GetOperationSeqForTransportReq(StationIDType SourseStnID,StationIDType DestiStnID,
                OneStageOpType  *CurTransportPlan)

to be this 是这样的

void GetOperationSeqForTransportReq(
  StationIDType SourseStnID,
  StationIDType DestiStnID,
  OneStageOpType CurTransportPlan)

For completeness also change this 为了完整性也改变了这一点

OneStageOpType CurOperPlan={0};

to be 成为

OneStageOpType CurOperPlan = {{0}};

Inside the function, you need: 在函数内部,您需要:

(*CurTransportPlan)[0].DevID1=5;  // This is Ok
(*CurTransportPlan)[1].DevID1=5;

This is because CurTransportPlan is a pointer to your array. 这是因为CurTransportPlan指向数组的指针 So you must dereference it to get the array. 因此,您必须取消引用它才能获得数组。 Then you can apply array indexing to the array. 然后,您可以将数组索引应用于数组。

Link to working example 链接到工作示例


To explain why the first one seemed to work, we can rewrite the code applying the equivalence transformations X->Y = (*X).Y , and X[N] = *(X+N) . 为了解释为什么第一个似乎有效,我们可以重写应用等价变换X->Y = (*X).YX[N] = *(X+N)

The correct indexing in array notation should be CurTransportPlan[0][0].DevID1 and CurTransportPlan[0][1].DevID1 ; 数组表示法中的正确索引应该是CurTransportPlan[0][0].DevID1CurTransportPlan[0][1].DevID1 ; however your code swapped the indices in both cases. 但是你的代码在两种情况下都交换了索引。 This meant the first one still worked but the second one broke. 这意味着第一个仍然有效但第二个仍然有效。


Regarding your code design: it's been noted that there are two ways to approach passing an array to a function: 关于你的代码设计:已经注意到有两种方法可以将数组传递给函数:

  1. You can pass a pointer to the first element of the array, as alk suggests (passing the length separately, or hard-coding it) 您可以将指针传递给数组的第一个元素,如alk建议的那样(分别传递长度或硬编码)

  2. You can pass a pointer to the entire array as you are currently doing. 您可以像现在一样将指针传递给整个数组。

I will list some of the differences between these two designs. 我将列出这两种设计之间的一些差异。 Using your design, ie the pointer-to-whole-array: 使用您的设计,即指针到整个数组:

  • You get a compilation error if an array with a different number of rows is passed. 如果传递具有不同行数的数组,则会出现编译错误。
  • You get a compilation error if a non-volatile array is passed. 如果传递非易失性数组,则会出现编译错误。
  • You must write (*ptr) instead of ptr , which is slightly more verbose. 你必须写(*ptr)而不是ptr ,这稍微冗长一点。

The decision for you to make is whether you want the compiler to give errors in those first two points. 你决定是否希望编译器在前两点给出错误。 The errors can be avoided by casting, but generally speaking, need to cast is a sign that the other approach would have been a better design. 铸造可以避免错误,但一般来说,需要施放是另一种方法更好的设计标志。

If this function is only ever to be used with the size- 3 volatile array then IMHO your current approach is the best one, invoking maximum compiler detection of errors. 如果此函数只能与size- 3 volatile数组一起使用,那么IMHO当前的方法是最好的,调用最大的编译器检测错误。

As you have a typedef of three elements array: 因为你有一个typedef的三个元素数组:

typedef volatile struct OneStageOpTag {
  // ...
} OneStageOpType[3];

The problem looks to be: 问题似乎是:

void GetOperationSeqForTransportReq(
  StationIDType SourseStnID,
  StationIDType DestiStnID,
  OneStageOpType *CurTransportPlan)       // HERE

In above function you are declaring parameter as a pointer to 3-element array, but you should pass it as a pointer to first element of array. 在上面的函数中,您将参数声明为指向3元素数组的指针,但您应该将其作为指向数组第一个元素的指针传递。 This means you should do the following instead: 这意味着您应该执行以下操作:

typedef volatile struct _OneStageOpTag {
  // ...
} Array[3], Element;

// ...

Array CurOperPlan={0};
GetOperationSeqForTransportReq(1,1,&CurOperPlan);

// ...

void GetOperationSeqForTransportReq(StationIDType SourseStnID,
  StationIDType DestiStnID, Element *CurTransportPlan)
{
  NOP();
  CurTransportPlan[0]->DevID1=5;  // This is Ok
  CurTransportPlan[1]->DevID1=5;  // This is Not working      
}

This should works fine for you :) 这应该适合你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM