Tag: struct

使用c中的链表制作strcpy函数

我使用链表创建自己的strcpy函数但无法完成。 没有使用链表,它可能是这样的 char* cp2014strcpy(char * dest_ptr, const char * src_ptr) { char* strresult = dest_ptr; if((NULL != dest_ptr) && (NULL != src_ptr)) { while (NULL != src_ptr) { *dest_ptr++ = *src_ptr++; } *dest_ptr = NULL; } return strresult; } 但我无法得到如何使用链表制作strcpy。

将项目转换为C中链接列表的末尾

编辑*(晚上8:14) – 抱歉,我更正了我的代码并将其作为一种方法,因此可以更容易理解。 在添加到链表的末尾时,我不确定如何正确地转换结构。 编译此代码会在最后一行给出一个强制警告。 这可能是我的其余代码无法正常运行的原因。 例如: #include typedef struct { int data; struct node *next; } node; node *HEAD = NULL; node *addNode(int num) { if (HEAD == NULL) { HEAD = (node *)malloc(sizeof(node)); HEAD->next = NULL; HEAD->data = num; } else { node *newNode; newNode = (node *)malloc(sizeof(node)); newNode->data = num; newNode->next = […]

C snprintf追加struct member char *

当我打印出result-> value时,我得到垃圾(来自其他内存区域的文本)和free():无效指针 我试图安全地将一个字符串附加到现有的char *(结果的值成员,其结果是一个实例) unsigned const NAME_BUFFER=100; unsigned const VALUE_BUFFER=1000; typedef struct { char *name; int ID; char *value; } props; … static Bool myfunc(props *result) { unsigned char *pointer; result->name=malloc(NAME_BUFFER); result->value=malloc(VALUE_BUFFER); // not sure I need to do this, previous instances of the code produced // output such as the quick…(null)someoutput // which I […]

malloc灾难性地失败了

我正在尝试在C中实现Queue。来自Java和其他托管语言,我真的在努力进行内存管理。 这是enqueue()函数: int enqueue(Queue q, int value) { Node newNode = malloc(sizeof(Node)); /*newNode->value = value; if (q->size == 0) q->head = newNode; else q->head->next = &newNode; q->size++;*/ } 我收到此错误: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) – 1) * 2])) – __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) […]

将数据存储在程序中而不是外部文件中

我有以下C代码的一部分,它使用来自文件名WMM.COF的数据,并使用存储在文件中的数据来计算地球的磁场。 该程序工作正常,但我不能让程序访问外部文件; 我想将所有数据都存储在程序中。 我尝试使用结构数组来复制数据,然后将数组放入一个字符串,但这会导致程序出错并且不会产生正确的结果。 这是我正在尝试修改的程序的代码。 static void E0000(int IENTRY, int *maxdeg, double alt, double glat, double glon, double time, double *dec, double *dip, double *ti, double *gv) { static int maxord,i,icomp,n,m,j,D1,D2,D3,D4; static double c[13][13],cd[13][13],tc[13][13],dp[13][13],snorm[169], sp[13],cp[13],fn[13],fm[13],pp[13],k[13][13],pi,dtr,a,b,re, a2,b2,c2,a4,b4,c4,epoch,gnm,hnm,dgnm,dhnm,flnmj,otime,oalt, olat,olon,dt,rlon,rlat,srlon,srlat,crlon,crlat,srlat2, crlat2,q,q1,q2,ct,st,r2,r,d,ca,sa,aor,ar,br,bt,bp,bpp, par,temp1,temp2,parp,bx,by,bz,bh; static char model[20], c_str[81], c_new[5]; static double *p = snorm; char answer; FILE *wmmdat; wmmdat = […]

如何从文件中获取替代行并将其作为字符串存储到结构中?

我有一个文件需要通过代码读取。 该文件如下所示。 文件的第一行包含一个整数,表示文件中的日记条目数。 我需要编写一个C程序来读取文件并将内容存储在动态分配的结构数组中。 4 12/04/2010 Interview went well I think, though was told to wear shoes. 18/04/2010 Doc advised me to concentrate on something… I forget. 03/05/2010 Was asked today if I was an art exhibit. 19/05/2010 Apparently mudcakes not made of mud, or angry wasps. 我能够strtok()将日期,月份和年份存储在我的结构中,但是我坚持将字符串保存到我的结构中。 这是我的strtok()代码, FILE* file=fopen(“struct.txt”,”r”); if (file==NULL){ perror(“Error opening […]

从另一个头文件中的一个头文件构造

这段时间我已经很长时间了,但仍然没有看到哪里可能是个问题。 我们有头文件Player.h #ifndef PLAYER_H_ #define PLAYER_H_ typedef struct player { char *name; int gameId; int socketfd; int points; int state; }player_struct; #endif /* PLAYER_H_ */ 让我们有第二个头文件Game.h #ifndef GAME_H_ #define GAME_H_ #include “Player.h” #define NUMBEROFPLAYERS 2 typedef struct game { //Here }game_struct; #endif /* GAME_H_ */ 我的typedef的目的是使用类似这样的类型:player_struct: 这是源文件Player.c – 这是有效的。 #include “Player.h” player_struct *create_player { player_struct […]

在程序结束时检测到堆栈粉碎

我正在测试一个较小规模的程序,以便在我尝试通过return 0;退出程序时区分问题return 0; 在主要function的最后。 MAIN.C #include #include #include “Header.h” int main (void) { int i; int Fin = 0; Student sStu; Array aAry; Student *Stu = &sStu; Array *Ary = &aAry; InitArray(Ary, 1); while(Fin != 2) { printf(“Please choose a selection.\n”); printf(“1. Add Student\n”); printf(“2. Print Students\n”); printf(“3. Exit\n”); scanf(“%d”, &i); switch(i) { case 1: […]

用C ++写一个文件的不同结构?

我需要一种方法将三种不同类型的结构写入二进制文件,以后必须进行搜索。 (例如,结构A有两个字段,一个是int和一个char; struct B有一个int和一个long;我需要输出所有结构,其int等于键盘给出的结构)。 我理解如何在文件中编写相同类型的结构以及如何搜索它们,但在这里我只是迷失了,我想出的最好的事情是声明一个包含所有可能需要的字段的结构并留下我不需要的字段空的,但它确实感觉不对,有一个更好的方法来做到这一点。 我读过关于二进制文件的内容并且找不到任何相关内容,大多数示例和教程都涉及编写一种数据类型。 有人能指出我正确的方向吗? 编辑:我正在寻找@Jerry_coffin所谓的数据库模式,可能会使用现有的数据库系统之一,最好的方法。 谢谢大家的建议

malloc和free动态变化的结构

我在动态变化的结构中移动指针时遇到了麻烦。 我已经创建了我的代码,你可以在malloc中获得更多的内存,这看起来很有效。 我遇到的问题是如何添加到结构,如何释放内存以及如何从结构移动到结构并打印所有项目。 我正在尝试测试添加和打印(删除function,似乎没有工作,segfaults) 当我添加到结构然后打印结构时,我从我添加的值中获得了一个段错误。 我不知道我是否正确地从第一个结构转移到下一个结构。 #include #include #include “pointer.h” /******************************************** Creates more memory for size (strut * rec+1) *********************************************/ employee *create(int record){ employee *new_employee = malloc(sizeof(employee) * (record+1)); return new_employee; } /******************************************** Copies the data from one structure to a new structure with size “structure” multipled by rec+1 ***********************************************/ employee *copy(employee *data, int record){ […]