Tag: 结构

C struct自动初始化值,数组初始化

如果我有两个结构 typedef struct { int number_of_lines; char lines[MAX_CHAPTER_LINES][MAX_STR_SIZE + 1]; } Chapter; typedef struct { char name[MAX_STR_SIZE + 1]; int number_of_chapters; Chapter chapters[MAX_CHAPTERS]; } Book; 我创建了一个章变量: Chapter x1; 它的两个成员的价值将被初始化为什么? 它是垃圾吗? 还是零? 在我的代码中,我得到0为int ,但我的TA告诉我这将是垃圾? 另外,如果我要声明一个章节数组: Chapter chapters[30]; 是否会填充30个具有0 / NULL值元素的结构? 或者用垃圾值元素初始化?

动态结构数组中的malloc动态数组

typedef struct { char *word; } STR; int main() { STR *arr=(STR*)malloc(5*sizeof(*arr)); STR[2].word=(char*)malloc(200*sizeof(char)); STR[2].word=(char*)realloc(,400*sizeof(char)); return 0; } 这将无法正常工作并编写许多错误。 如何在动态结构数组中动态分配数组?

如何使用动态改变数据大小的结构?

仅针对C,C ++和向量的问题不能解决问题。 我有这样的结构: typedef __packed struct Packet_s { U8 head; U16 len; U32 id; U8 data; U8 end; U16 crc; } Packet_t, *Packet_p; ( 编辑 :U8是uint8_t(unsigned char)等等) 例如,我收到了数据包(hex): 24 0B 00 07 00 00 00 AA 0D 16 1C 哪里 head = 0x24 len = 0x0B 0x00 id = 0x07 0x00 0x00 0x00 data = […]

我们如何使用结构?

我遇到的主要问题是有这么多参数,我只是想摆脱它,是的,我不理解结构的逻辑。 然而,它变得更加清晰…… 编辑所以cHao希望我使用一个特定的案例,所以这里是我做的一个例子: #include int main() { //Top coord. of the square int top_x1 = 0; int top_y1 = 10; int top_x2 = 10; int top_y2 = 10; //Bottom coord. of the square int bottom_x1 = 0; int bottom_y1 = 0; int bottom_x2 = 10; int bottom_y2 = 0; //Left coord. of the square int […]

C – 字节数组结构(dns查询)

我有这些结构: typedef struct dnsQuery { char header[12]; struct TdnsQuerySection *querySection; } TdnsQuery; typedef struct dnsQuerySection { unsigned char *name; struct TdnsQueryQuestion *question; } TdnsQuerySection; typedef struct dnsQueryQuestion { unsigned short qtype; unsigned short qclass; } TdnsQueryQuestion; 我从recvfrom有字节数组的dns查询。 我试图从字节数组获取结构,如下所示: TdnsQuery* dnsQuery = (TdnsQuery*)buf; printf(“%u”, dnsQuery->querySection->question.qtype); 为什么我得到错误解除指向不完整类型的指针? 我这样做了吗? 或者如何从该数组中获取dns查询结构? 我需要dns查询问题和类型。

为什么结构不能成为自己的成员?

我有一个结构foo 。 声明foo*类型的成员: typedef struct foo { struct foo* children[26]; } foo; 但是如果我尝试声明foo类型的成员,我会收到一个错误: typedef struct foo { struct foo children[26]; } foo; 这个声明给了我错误 ‘struct foo’的定义在结束’}’之前是不完整的

有关C结构顺序的保证吗?

我已经广泛使用了结构,并且我看到了一些有趣的东西,特别是*value而不是value->first_value ,其中value是指向struct的指针, first_value是第一个成员, *value安全? 另请注意,由于对齐,无法保证大小,基于结构/寄存器大小的alginment值是什么? 我们将数据/代码对齐以便更快地执行,我们可以告诉编译器不要这样做吗? 那么也许我们可以保证结构的某些东西,比如尺寸? 当对结构成员进行指针运算以便定位成员偏移时,我认为你做了-如果对于大端的小端+ ,或者它只是依赖于编译器? malloc(0)真正分配了什么? 以下代码用于教育/发现目的,并不意味着具有生产质量。 #include #include int main() { printf(“sizeof(struct {}) == %lu;\n”, sizeof(struct {})); printf(“sizeof(struct {int a}) == %lu;\n”, sizeof(struct {int a;})); printf(“sizeof(struct {int a; double b;}) == %lu;\n”, sizeof(struct {int a; double b;})); printf(“sizeof(struct {char c; double a; double b;}) == %lu;\n”, sizeof(struct {char c; double […]

编写C struct的init函数

所以这是我在头文件中的结构: struct _Variable { char *variableName; char *arrayOfElements; int32_t address; }; typedef struct _Variable Variable; 这是我在.c文件中的init函数的实现: void initVariable(Variable *variable, char *variableName, char *arrayOfElements, int32_t address) { int lengthOfVariableNameWithTerminatingChar = strlen(variableName) + 1; variable->variableName = malloc( sizeof(char) * lengthOfVariableNameWithTerminatingChar); strncpy(variable->variableName, variableName, lengthOfVariableNameWithTerminatingChar); int lengthOfArrayOfElementsWithTerminatingChar = strlen(arrayOfElements) + 1; variable->arrayOfElements = malloc( sizeof(char) * lengthOfArrayOfElementsWithTerminatingChar); strncpy(variable->arrayOfElements, arrayOfElements, […]

C结构初始化与变量

我遇到了C89之后似乎没有通过任何C标准解决的问题,除非提到结构初始化限制已被解除。 但是,我使用Open Watcom IDE(用于调试)遇到错误,其中编译器声明初始化程序必须是常量表达式。 这是正在发生的事情的要点。 typedef struct{ short x; short y; } POINT; void foo( short x, short y ) { POINT here = { x, y }; /* <– This is generating the error for the compiler */ /* … */ } 任何想法为什么,或什么标准不允许?

在socket上发送struct时序列化问题

我正在开发基于UDP的客户端/服务器我想从服务器向客户端发送不同的消息。 为每条消息定义了不同的C结构。 我想了解我序列化数据的方式有什么问题。 struct Task { int mType; int tType; int cCnt; int* cId; char data[128]; }; 序列化/反序列化function unsigned char * serialize_int(unsigned char *buffer, int value) { buffer[0] = value >> 24; buffer[1] = value >> 16; buffer[2] = value >> 8; buffer[3] = value; return buffer + 4; } unsigned char * serialize_char(unsigned char […]