Tag: struct

malloc结构指针数组与结构数组

有什么区别 struct mystruct *ptr = (struct test *)malloc(n*sizeof(struct test)); 和 struct mystruct **ptr = (struct test *)malloc(n*sizeof(struct test *)); 他们都工作正常,我只是好奇两者之间的实际差异。 第一个是否分配了一个结构数组,而第二个是结构指针数组? 另一种方式? 另外,哪一个占用内存较小?

结构对齐填充,最大填充大小和结构成员的顺序

我一直在学习结构数据填充,因为我发现我的sizeof()运算符没有返回我的预期。 根据我观察到的模式,它将结构成员与最大的数据类型对齐。 所以例如…… struct MyStruct1 { char a; // 1 byte char b; // 1 byte char c; // 1 byte char d; // 1 byte char e; // 1 byte // Total 5 Bytes //Total size of struct = 5 (no padding) }; struct MyStruct2 { char a; // 1 byte char b; // […]

如何解决“参数有不完整类型”的错误?

我是新手,我需要帮助调试我的代码。 当我编译它”forms参数1的类型不完整’和forms参数2的类型是不完整的’错误出现在 printf(“Age is %d years.\n”, calc_age(birth, current)); 而’参数1(’出生’)具有不完整类型’和’参数2(’当前’)具有不完整类型’错误出现在 int calc_age (struct date birth, struct date current) { 感谢帮助,谢谢! #include int calc_age (struct date birth, struct date current); int main(void) { struct date { int month[1]; int day[1]; int year[1]; }; struct date birth, current; char c; printf(“Input the birthdate (MM/DD/YY): “); scanf(“%d %c %d […]

实现链表时指针奇怪的问题

我正在尝试在C中实现链接列表,并且我想将头节点存储在单独的结构中。 但是,每当我添加另一个节点时,似乎都会以某种方式重新分配头节点。 #include #include struct BC_node { struct BC_node *next; void *data; }; struct BC_list { struct BC_node *head; struct BC_node *tail; }; void BC_list_push(struct BC_list *list, void *data) { struct BC_node *node = calloc(1, sizeof(struct BC_node)); if (list->head != NULL) printf(“head: %d\n”, *((int *) (list->head)->data)); node->next = NULL; node->data = data; if (list->head == […]

每次运行函数时都要进行Malloc’ing并重置数组

struct variables { unsigned int counter; char *bra; unsigned int maxb; int *findtheking; unsigned int numoright; }; int getlen = 0; // I give getlen a value in another function int solo = 0; mat.bra = (char*)malloc(sizeof(char)*getlen); mat.bra = ‘\0’; struct variables pal = { 0, ‘\0’, 0, 0, 0 }; struct variables mat = […]

C中的循环定义

我写的是: typedef enum _MyStatus { MY_STATUS_OK = 0, MY_STATUS_GENERAL_ERROR = -1, } MyStatus; typedef MyStatus (*MyCallback)(MySettings *settings); typedef struct _MySettings { MyCallback callback; } MySettings 但是,它不会像定义MyCallback时那样编译它不知道MySettings。 如果我交换MySettings和MyCallback,那将是另一回事:MySettings不会知道MyCallback。 在C中处理这类问题有多普遍? 谢谢!

从一个标头转换为另一个标头中的结构

我遇到了麻烦…我有这个标题: #ifndef PESSOA_H #define PESSOA_H typedef struct pa{ int idade; int atend; }pessoa; void inicPessoa(pessoa *ps, int id); #endif 并且,在filaC.h中: #ifndef FILAC_H #define FILAC_H #include “pessoa.h” typedef struct pessoa elem_t; typedef struct no{ elem_t info; struct no *prox, *ant; } No; typedef No * Fila; #endif 但是编译器说filaC.h上的fiel信息有一个不完整的类型。 改变elem_t info; 将struct elem_t into; 没有效果。

MARS MIPS和struct节点

typedef struct node { int data; struct node *next; } nodeL; 假设我想用MIPS汇编语言翻译上述声明,我该怎么做呢? 除了在.text段中分配内存(使用系统调用9)之外, .data段怎么样? 对齐怎么样?

使用memcpy将数据从缓冲区存储到struct中

我有sflow数据包捕获代码,我需要从缓冲区打印sflow数据信息。 我已经为所需信息定义了结构,并尝试使用memcpy将缓冲区信息复制到结构中。 当我打印字段时,我得到了一些不正确的值。 附加了下面的结构代码: typedef unsigned char mac[6]; typedef unsigned char ip_v4[4]; typedef unsigned char ip_v6[16]; typedef unsigned int header_protocol; /* Packet header data */ const MAX_HEADER_SIZE = 256; /* The maximum sampled header size. */ struct sampled_header { header_protocol protocol; /* Format of sampled header */ unsigned int frame_length; /* Original length of packet […]

给出pid打印子进程(MINIX)

我正在研究一个项目,作为其中的一部分,我需要在MINIX中实现系统调用/库函数。 作为其中的一部分,我需要能够使用其pid打印给定进程的子进程列表。 我想我已经找到了我需要的部分内容,但是我坚持让它与一个给定的pid一起工作。 struct task_struct *task; struct list_head *list; list_for_each(list, &current->children) { task = list_entry(list, struct task_struct, children); } 这看起来像我需要的东西吗? 我知道为了让我使用pid我需要使用: struct task_struct find_task_by_pid(pid_t pid); 但结合上述内容并不是我以前做过的事情。