简体   繁体   English

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

[英]Accessing a typedef structure inside another typedef struct

Kindly, see the below given snippet. 请参见下面给出的代码段。 How can we access the descrptn from the struct sample , using the struct storage ? 我们如何使用struct storage访问struct sampledescrptn

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

Your question How can we access the descrptn from the struct sample, using the struct storage? 您的问题我们如何使用结构存储访问结构示例中的descrptn? is addressed in the main function of the corrected code below, but because of some incorrect assumptions on the shape of your compound struct, it may not look like what you assumed it would. 可以在下面的更正代码的主要功能中解决该问题,但是由于对复合结构的形状存在一些不正确的假设,因此可能看起来与您假定的情况不同。

First there are some syntax errors and wrong assumptions that need to be addressed. 首先,有一些语法错误和错误的假设需要解决。

1) - Note: Because you are initializing an element of a constant expression to 0xFF, even though its alignment within the initializer should identify it as a signed char , depending on your compiler and its settings, it might assume an unsigned char and warn of an overflow. 1) -注意:由于要将常量表达式的元素初始化为0xFF,即使在初始化程序中将其对齐应将其标识为带signed char ,具体取决于您的编译器及其设置,它也可能会假定为unsigned char并发出警告溢出。 (This is precisely what happens on my system). (这正是在我的系统上发生的事情)。 Because the value is actually aligned with a signed char , at run-time an overflow should not occur. 因为该值实际上与带signed char对齐,所以在运行时不应发生溢出。
2) - You have an array of struct . 2) -您有一个struct数组。 Although a simple array can be initialized with a variable ( the Variable Length Array concept has been valid since C99. ), A struct can only be initialized with constant values. 尽管可以使用变量来初始化简单数组( 可变长度数组的概念自C99开始有效),但是只能使用常量值来初始化结构 Because your code attempts to initialize a struct ( sample ) with a variable, it should fail to compile. 因为您的代码尝试使用变量初始化结构( sample ),所以它应该无法编译。
3) -The shape of a struct initializer must match the shape of the struct it is initializing, including initializer position and type. 3) -结构体初始化程序的形状必须与它正在初始化的结构体的形状匹配,包括初始化程序的位置和类型。 Because TypePara is a compound struct (a struct containing a struct member) its initializer must take that into account. 由于TypePara是复合结构(包含结构成员的结构),因此其初始化程序必须考虑到这一点。 Your initializer for const TypePara storage[] ={...} is not shaped correctly. 您的const TypePara storage[] ={...}初始化程序的形状不正确。

The first two items should be clearly flagged for you with compile time messages. 前两个项目应在编译时消息中为您明确标记。 Make sure you have your compiler set to see them. 确保已设置编译器以查看它们。

The third item, unfortunately will not always show up as an error, or warning. 不幸的是,第三项不会总是显示为错误或警告。 C will sometimes let you do things that are not necessarily correct. C有时会让您做不一定正确的事情。

Each of these is addressed below, in syntax and comments. 以下分别以语法和注释解决了所有这些问题。

Using your struct definitions, with the following indicated changes, you can access descrptn like this: (The following is a complete and compilable adaptation of your original post) 使用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;
}

If you want hex values, you should have unsigned char or uint_8 , and not char (0xff is negative). 如果需要十六进制值,则应使用unsigned charuint_8 ,而不要使用char(0xff为负数)。 Also i suppose you only want a TypeText in each TypePara and not a array, but this should help to fix your code. 此外,我想你只需要在每一个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;
}

In the initialization of the storage array, the type of sample is a "pointer to a const TypeText" . storage阵列的初始化中, sample的类型是“指向const TypeText的指针” Thus, to make that initialization work, you need a pointer to a const Typetext in the TypePara structure. 因此,为了使初始化工作正常进行,您需要一个指向TypePara结构中的const Typetext的指针。 So the definition of the TypePara structure should look like this 所以TypePara结构的定义应该像这样

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

Here's a complete example: 这是一个完整的示例:

#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" );
    }
}
storage[index].test[index].descrptn

应该管用。

Actually I found my solution, in this way: 实际上,我以这种方式找到了解决方案:

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

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

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