Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 在另一个typedef结构中访问typedef结构_C_File_Struct_Header_Typedef - Fatal编程技术网

C 在另一个typedef结构中访问typedef结构

C 在另一个typedef结构中访问typedef结构,c,file,struct,header,typedef,C,File,Struct,Header,Typedef,请看下面给出的代码片段。我们如何使用结构存储,从结构示例访问描述 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 s

请看下面给出的代码片段。我们如何使用
结构存储
,从
结构示例
访问
描述

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}   
};
应该有用


应该有用

如果需要十六进制值,则应具有
无符号字符
uint_8
,而不是字符(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;
}
#包括
类型定义结构{
无符号字符十六进制;
char descrptn[50];
}打字文本;
类型定义结构{
int-val;
打字文本*测试;
}TypePara;
静态TypeText示例[]={
{0x00,“低”},
{0x7F,“Mid”},
{0xFF,“高”}
};
TypePara存储[]={
{0,&sample[0]},
{1,&示例[1]}
};
int main()
{
printf(“%s\n”,存储[0]。测试->描述);
返回0;
}

如果需要十六进制值,则应具有
无符号字符
uint_8
,而不是字符(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;
}
#包括
类型定义结构{
无符号字符十六进制;
char descrptn[50];
}打字文本;
类型定义结构{
int-val;
打字文本*测试;
}TypePara;
静态TypeText示例[]={
{0x00,“低”},
{0x7F,“Mid”},
{0xFF,“高”}
};
TypePara存储[]={
{0,&sample[0]},
{1,&示例[1]}
};
int main()
{
printf(“%s\n”,存储[0]。测试->描述);
返回0;
}
您的问题:我们如何使用结构存储从结构示例访问descrptn?在下面更正代码的主要功能中解决了这个问题,但由于对复合结构形状的一些错误假设,它可能与您假设的不一样

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

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

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

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

下面以语法和注释的形式介绍了每一个问题

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

您的问题是,我们如何使用struct存储从struct示例访问descrptn?在下面更正代码的主要功能中解决了这个问题,但由于对复合结构形状的一些错误假设,它可能与您假设的不一样

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

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

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

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

下面以语法和注释的形式介绍了每一个问题

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


存储
数组的初始化过程中,
样本
的类型是“指向常量TypeText的指针”。因此,要使初始化工作,您需要一个指向中的
常量Typetext
的指针
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;
}
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" );
    }
}
        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;
        }