访问另一个typedef结构中的typedef结构

时间:2016-10-27 18:29:38

标签: c file struct header typedef

请注意,请参阅下面给出的代码段。我们如何使用descrptnstruct sample访问struct storage

typedef struct {
char hex;
char descrptn[50];
}TypeText;

typedef struct {
int val;
TypeText test[10];
}TypePara;

static const TypeText sample[]={
{0x00,  "Low"},
{0x7F,  "Mid"},
{0xFF,  "HIgh"},
};

const TypePara storage[]= {
   {0, sample},
   {1, sample}   
};

5 个答案:

答案 0 :(得分:2)

您的问题我们如何使用结构存储从结构示例访问descrptn?在下面更正的代码的主函数中解决,但由于对形状的一些不正确的假设你的复合结构,它可能看起来不像你想象的那样。

首先,需要解决一些语法错误和错误的假设。

1) - 注意:因为您正在将常量表达式的元素初始化为0xFF,即使它在初始化程序中的对齐应该将其标识为signed char,这取决于您的编译器及其设置,它可能会假设unsigned char并警告溢出。 (这正是我系统上发生的事情)。因为该值实际上与signed char对齐,所以在运行时不应发生溢出 2) - 您有一个struct数组。虽然可以使用变量初始化一个简单的数组( Variable Length Array 概念自C99以来一直有效。),A struct 只能用常量值初始化。因为您的代码尝试使用变量初始化结构(sample),所以它应该无法编译 3) - struct初始化程序的形状必须与它初始化的结构的形状相匹配,包括初始化程序的位置和类型。因为TypePara是复合结构(包含结构成员的结构),所以它的初始值设定项必须考虑到这一点。 const TypePara storage[] ={...}的初始化程序形状不正确。

应该使用编译时消息清楚地标记前两项。确保已设置编译器以查看它们。

不幸的是,第三项并不总是显示为错误或警告。 C有时会让你做一些不一定正确的事情。

下面将在语法和注释中解决其中的每一个问题。

使用您的struct定义,并进行以下指示的更改,您可以像这样访问descrptn :(以下是对您原始帖子的完整且可编辑的修改)

typedef struct {   //Note:                                             
char hex;          //Initializer for TypeText shaped like this:        
char descrptn[50]; //hex         description    
}TypeText;         //{0x01,      "one"}; 

typedef struct {   //Note:  TypePara is a struct containing an array of struct
int val;           //Initializer for each instance of TypePara shaped like this:
TypeText test[10]; //val  TypeText[0]   TypeText[1]     TypeText[9]
}TypePara;         //{1, {0x01, "one"},{0x02, "two"}...{0x0A, "Ten"}};

static const TypeText sample[]={

    {0x00,  "Low"},
    {0x49,  "Mid"},
  //{0xFF,  "HIgh"} //commented and replaced to 
    {0x7F,  "High"} //prevent possible overflow condition
};

//const TypePara storage[]= {
//   {0, sample}, //error, "sample is not a compile time constant
//   {1, sample}   
//};

//Note: illustrates shape only.  Values are otherwise meaningless
const TypePara storage[] = {
    { 0,{{0x00, "zero"},{0x01, "one"},{0x02, "two"},{0x03, "three"},{0x04, "four"},{0x05, "five"},{0x06, "six"},{0x07, "seven"},{0x08, "eight"},{0x09, "nine"}}},
    { 1,{{0x01, "zero"},{0x11, "one"},{0x12, "two"},{0x13, "three"},{0x14, "four"},{0x15, "five"},{0x16, "six"},{0x17, "seven"},{0x18, "eight"},{0x19, "nine"}}},
    { 2,{{0x02, "zero"},{0x21, "one"},{0x22, "two"},{0x23, "three"},{0x24, "four"},{0x25, "five"},{0x26, "six"},{0x27, "seven"},{0x28, "eight"},{0x29, "nine"}}},
    { 3,{{0x03, "zero"},{0x31, "one"},{0x32, "two"},{0x33, "three"},{0x34, "four"},{0x35, "five"},{0x36, "six"},{0x37, "seven"},{0x38, "eight"},{0x39, "nine"}}},
    { 4,{{0x04, "zero"},{0x41, "one"},{0x42, "two"},{0x43, "three"},{0x44, "four"},{0x45, "five"},{0x46, "six"},{0x47, "seven"},{0x48, "eight"},{0x49, "nine"}}}
};


int main(void)
{
    //Accessing descrptn   
    printf( "descrptn is %s\n", storage[0].test[0].descrptn);
    printf( "descrptn is %s\n", storage[0].test[1].descrptn);
    printf( "descrptn is %s\n", storage[0].test[2].descrptn);
    printf( "descrptn is %s\n", storage[0].test[3].descrptn);
    //...
    //This can continue as you have 5 storage elements
    //defined, each of those containing 10 test elements.

    return 0;
}

答案 1 :(得分:1)

如果你想要十六进制值,你应该有unsigned charuint_8,而不是char(0xff是负数)。另外我想你只想在每个 TypePara 中使用 TypeText 而不是数组,但这应该有助于修复你的代码。

#include <stdio.h>

typedef struct {
    unsigned char hex;
    char descrptn[50];
} TypeText;

typedef struct {
    int val;
    TypeText *test;
} TypePara;

static TypeText sample[]={

{0x00,  "Low"},
{0x7F,  "Mid"},
{0xFF,  "HIgh"}

};

TypePara storage[]= {
   {0, &sample[0]},
   {1, &sample[1]}   
};


int main()
{
    printf("%s\n", storage[0].test->descrptn);
    return 0;
}

答案 2 :(得分:1)

storage数组的初始化中,sample的类型是指向const TypeText“的”指针。因此,要使初始化工作,您需要指向const Typetext结构中的TypePara的指针。所以TypePara结构的定义应该如下所示

typedef struct
{
    int value;
    const TypeText *array;
}
TypePara;

这是一个完整的例子:

#include <stdio.h>

typedef struct
{
    unsigned char hex;
    char description[50];
}
TypeText;

typedef struct
{
    int value;
    const TypeText *array;
}
TypePara;

static const TypeText sample[]=
{
    { 0x00,  "Low"  },
    { 0x7F,  "Mid"  },
    { 0xFF,  "High" }
};

const TypePara storage[]=
{
    { 0, sample },
    { 1, sample }
};

int main( void )
{
    for ( int i = 0; i < 2; i++ )
    {
        printf( "storage with value %d:\n", storage[i].value );
        for ( int j = 0; j < 3; j++ )
            printf( "   hex=%02x description='%s'\n", storage[i].array[j].hex, storage[i].array[j].description );
        printf( "\n" );
    }
}

答案 3 :(得分:0)

storage[index].test[index].descrptn

应该有效。

答案 4 :(得分:0)

实际上我通过这种方式找到了我的解决方案:

        typedef struct {
            int age;
            int RollNo;
            int Rank;
            char Name[10];
        }TypeStudent;

        typedef struct {
            char class_name[20];
            TypeStudent *Students;
        }TypeClass;

        int main()
        {

             const TypeStudent  Stu_Details[] = {
             { 3,   1,  18, "Mahesh"},
             { 3,   1,   7,  "Kumar"}
            };

              const TypeClass Class_Details[]= {
             { "Class 10",     Stu_Details},  //two students details
             { "Class 8",                0}   //no student details attached
            };

            printf("\r\nTest: %d",Class_Details[0].Students->Rank);
            printf("\r\nTest: %d",(Class_Details[0].Students+1)->Rank);

            return 0;
        }