使用c++/vC++将结构数组和2d数组传递给函数

Passing array of structures and 2d array to a function using c++/vC++

本文关键字:数组 2d 函数 使用 vC++ 结构 c++      更新时间:2023-10-16

我需要帮助,因为我有一个Struct

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]);

这是正确的方式吗?否则请告诉我。

原型应该是

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);

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

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);
}