【问题标题】:Passing array of structures and 2d array to a function using c++/vC++使用 c++/vC++ 将结构数组和二维数组传递给函数
【发布时间】:2014-09-16 09:24:07
【问题描述】:

我需要帮助,我有一个结构

typedef struct {  
    unsigned char data0; 
    unsigned char data1; 
   //  like this 8 bytes of data in my structure 
} MyStruct;

typedef struct {
    Mystruct my_st[8];
} Info1;

typedef struct { 
    unsigned char My_Array[8][7];
} Info2;

//now i want to pass the  array of 8 structures in Info1 and 2D array of Info2 .
My_Function( &Info1->my_st[8], &Info2->My_Array[8][7]);

这是正确的方法,否则请告诉我。

【问题讨论】:

  • My_Function( &Info1, &Info2);?这会将结构传递给 My_Function。 MyStruct的定义不正确。

标签: c++ c arrays visual-c++ character-arrays


【解决方案1】:

原型应该是

void My_Function(MyStruct (&my_st)[8], unsigned char (&My_Array)[8][7]);

然后这样称呼它:

Info1 info1;
Info2 info2;
My_Function(info1.my_st, info2.My_Array);

但这样会更简单:

void My_Function(Info1 &info1, Info2 &info2);

和

Info1 info1;
Info2 info2;
My_Function(info1, info2);

【讨论】:

    【解决方案2】:

    我认为这会对您有所帮助:

    typedef struct {
        unsigned char data0;
        unsigned char data1;
        //  like this 8 bytes of data in my structure 
    } MyStruct;
    
    typedef struct {
        MyStruct my_st[8];
    } Info1;
    
    typedef struct {
        unsigned char My_Array[8][7];
    } Info2;
    
    //declare My_Function
    void My_Function(MyStruct[], unsigned char[][7]);
    
    
    int main()
    {
        Info1 i;
        Info2 j;
        // use My_Function
        My_Function(i.my_st,j.My_Array);
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-15
      • 2014-05-28
      • 1970-01-01
      • 2021-05-10
      • 1970-01-01
      相关资源
      最近更新 更多