Kindly, see the below given snippet. How can we access the descrptn
from the struct sample
, using the 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}
};
Your question How can we access the descrptn from the struct sample, using the struct storage? 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. (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.
2) - You have an array of 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. Because your code attempts to initialize a struct ( sample
) with a variable, it should fail to compile.
3) -The shape of a struct initializer must match the shape of the struct it is initializing, including initializer position and type. Because TypePara is a compound struct (a struct containing a struct member) its initializer must take that into account. Your initializer for const TypePara storage[] ={...}
is not shaped correctly.
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.
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)
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). Also i suppose you only want a TypeText in each TypePara and not a array, but this should help to fix your code.
#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" . Thus, to make that initialization work, you need a pointer to a const Typetext
in the TypePara
structure. So the definition of the TypePara
structure should look like this
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.