Tag: 结构

为嵌套结构指针分配内存

我正在使用C代码生成器创建具有以下结构的头文件: typdef struct Place { struct Forest { int trees; } *Forest; } Place ; 并在c ++项目中使用它们。 当我尝试访问Place.Forest-> trees时,我得到一个段错误,因为Place.Forest是一个悬空指针。 我无法正确malloc它因为Place.Forest = malloc(sizeof(Place.Forest)); 只会返回指针的大小。 我不能使用Place.Forest=malloc(sizeof(struct Forest)); 因为我从C ++访问Place并且作用域使我无法看到Forest。 如何在不更改Place或un-nesting Forest的情况下为Forest分配内存? 由于自动生成大量代码,因此修改结构是不切实际的。

使用结构与c程序联系经理

struct contact { char name[20],email[20]; int hpnum; }add; int option; int main (void) { system(“cls”); printf(“==========Welcome to Jeffery’s Contact System Management==========\n”); printf(“\t\t\tContact System Main Menu\n”); printf(“[1] Create a New Contact\n”); printf(“[2] Modified Existing Contact\n”); printf(“[3] Delete Existing Contact\n”); printf(“[4] Search Existing Contact\n”); printf(“[5] Exit\n”); printf(“Please enter one of your option.\n”); scanf(“%d”,option); switch(option) { //add new […]

有关char数组的struct的问题

下面是我的代码片段 struct encode { char code[MAX]; }a[10]; int main() { char x[]={‘3′,’0′,’2′,’5′,’9′,’3′,’1’}; for(i=0;i<1;i++) { printf("%c",x[i]); //This will printout like 3025931 now I want this to be stored in structure. } strcpy(a[0].code,x); // or a[0].code=x;//neither works display(); } void display() { printf("%c",a[0].code); } 我希望输出像:3025931。 由于不兼容的分配类型,我没有得到。 请告诉我哪里出错了。

使用union名称访问union成员

如何访问结构中存在的union成员? 考虑代码段: struct Emp { char name[20]; union address { char addr[50]; }; }; struct Emp e; 使用e ,如何在不创建任何联合对象的情况下访问addr类型?

如何使用Union在1 C数组中存储2个不同的结构

我想创建一个数组来存储2种类型的C结构 – Employee ,以及它的’child’, Manager 。 我创建了一个联合Person来保存其中任何一个,然后尝试用它创建一个数组,但它不起作用。 我怎样才能让这样的arrays工作? 相关代码如下。 typedef struct { char name[20]; double salary; } Employee; //Manager struct inheriting from employee struct typedef struct { Employee employee; int bonus; } Manager; typedef union{ Employee e; Manager m; } Person; Manager boss; Employee harry ; Employee tommy; Person staff[]; int main(void) { … boss […]

将数据从csv文件读入struct,得到错误

我正在尝试将csv文件中的数据读入结构中。 该结构包含int,char和float成员。 我收到错误,除了char成员。 我对C很新,所以感谢你的帮助! 来自csv文件“订单”的数据: 0, cafe, 3.90, 0 0, espresso, 3.50, 0 … 我的结构: typedef struct { int position; char name[20]; float price; int counter; }drink; void init(drink *pt) { FILE *fp; char buf[50]; int i = 0, j; fp=fopen(“Order”, “r”); while( fgets(buf,sizeof(buf),fp) != NULL) { strcpy(pt[i].position, strtok(buf,”,”)); strcpy(pt[i].name, strtok(NULL,”,”)); strcpy(pt[i].price, strtok(NULL,”,”)); strcpy(pt[i].counter, strtok(NULL,”,”)); […]

C中的结构,编译器错误

我收到此错误: str.c:5:19: error: expected identifier or ‘(‘ before ‘struct’ 编译以下代码时。 这有什么问题? #include struct addpoints (struct point p1, struct point p2){ p1.x += p2.x; p1.y += p2.y; return p1; } int main(){ struct point{ int x; int y; }; struct point p1 = { 13, 22 }; struct point p2 = { 10, 10 }; addpoints […]

C:数组结构中的结构数组

我有这些结构: struct menu_item{ int id; char *text; }; struct menu_tab{ char *label; unsigned char item_count; struct menu_item *items; }; struct menu_page{ char *label; unsigned char tab_count; struct menu_tab *tabs; }; struct Tmenu{ unsigned char page_count; struct menu_page *pages; }; 我想定义整个菜单系统: struct Tmenu menu_test = { 2, { “F1”, 2, { { “File”, 8, { {1, […]

解释包含结构的联合的sizeof运算符的结果

#include struct mystruct { char cc; float abc; }; union sample { int a; float b; char c; double d; struct mystruct s1; }; int main() { union sample u1; int k; u1.s1.abc=5.5; u1.s1.cc=’a’; printf(“\n%c %f\n”,u1.s1.cc,u1.s1.abc); k=sizeof(union sample); printf(“%d\n\n”,k); return 0; } 运算符的大小返回8我仍然能够访问结构元素,一次多个,并且sizeof运算符仍然返回我假设的原始数据类型的最大大小。 为什么会这样? 实际分配的大小是8吗? 并且sizeof返回错误的值? 或者实际分配的大小是8 ? 那么结构是如何适应的? 如果我们使用malloc和sizeof分配一个联合数组,它会在这种情况下分配足够的空间吗? 请详细说明。

在C中使用a_list结构(共享内存中的链表)实现共享内存段

我需要创建一个共享内存段,使用我创建的结构来保存节点 struct a_list { //The head of the list that acts as a node, this list has a next* and a prev* struct list_head list; unsigned long* val; char* str; char state; }; 我尝试在堆栈上查找答案,但他们实现了一个Node而不是我的struct,这是一个包含节点的列表 我也有这个缓冲区和追加方法,我曾经附加到这个列表(使用malloc),但我知道对于共享内存我不能这样做 struct buffer { struct a_list* buffer[BUFFER_SIZE]; int in; int out; }; //Method to append a file to the end of […]