Tag: struct

使用唯一结构作为参数C的pthread

我有这段代码给我带来了麻烦。 我知道所有的线程都在读取相同的结构。 但我不知道如何解决这个问题。 #include #include #include typedef struct { int a,b; } s_param; void * threadfunc(void *parm) { s_param *param2 = parm; printf(“ID:%d and v:%d\n”,param2->a,param2->b); pthread_exit(NULL); } int main(int argc, char **argv) { pthread_t thread[3]; int rc=0,i; void * status; for(i=0; i<3 ; ++i){ s_param param; param.b=10; param.a=i; rc = pthread_create(&thread[i], NULL, threadfunc, &param ); […]

结构和指针指针

我正在学习链接列表以及如何使用结构和指针在C中创建它们。 我在下面有一个例子。 根据我的理解,被调用的push()传递结构的起始内存位置,其中头节点作为参数。 我们的push()函数的参数将struct节点作为指针的指针,因此它作为引用而不是实际副本传递。 因此,我们的struct node ** headref的第一个指针只是指向头节点的内存位置的指针,第二个指针指向该值,该值是头节点指向的下一个内存位置。 我们在struct节点中创建一个名为newnode的新节点,为它分配一些内存。 然后,我们在此节点内创建一个int类型数据。 好吧,假设我说的一切都是正确的,下一部分就是我对此感到困惑。 newNode->next= *headRef; 从我能理解的这一行取消引用headref所以这将使headref指向头节点。 然后我们有一个指针操作,其中headref指向的内容也将是我们的指针接下来指向的内容。 基于此,我们的新节点(newnode)中的下一个指针将指向头指针。 我也困惑的下一行是: *headRef = newNode; 取消引用的headref指针指向的是头节点,现在将指向我们的newnode。 基于此,应该有一个名为newnode的新节点,其中包含一个int数据,另一个指针将我们的newnode链接到头部。 然后headref指针(或者它是头节点?)将指向新节点。 我知道这是不正确的,因为我们的newnode旁边的指针应该指向第二个节点,所以我们的newnode可以在struct中链接。 我也不相信我理解上面两行代码中指针和解除引用的指针。 码: void Push(struct node** headRef, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = *headRef; *headRef = newNode; } void PushTest(void) { struct node* head […]

带指针的嵌套结构

#include #include #include typedef struct node *tree_ptr; typedef struct table * Table; struct node { char* element; tree_ptr left, right; }; typedef struct table { tree_ptr head; int tree_h; }table; char* key = NULL; Table insert(char* insert_key,Table t) { int height = 0; //tree_ptr ptr = t->head; tree_ptr *ptr = &(t->head); key = strdup(insert_key); tree_ptr […]

如何将结构列表/数组从python传递给C

我有一个C函数,必须可以从C和Python调用。 我无法弄清楚如何将c-cons结构的python列表传递给c函数,每个结构包含几个嵌套结构。 这些结构中的一个在python中看起来像这样: class STATION_MM_NODE(ctypes.Structure): _fields_ = [ (“signal”, MM_STRUCT), (“noise”, MM_STRUCT), (“signalWindowLen”, ctypes.c_double), (“metadata”, SAC_PZ) ] 在C中像这样: typedef struct stationMMnode { struct mantleMagStruct *signal; struct mantleMagStruct *noise; double signalWindowLen; SAC_PZ metadata; } stationMMnode_t; 采用stationMMnode结构数组的c函数可以调用为: double magnitudeCompute_Mw_Mm_Event(stationMMnode_t **stationMMarray, int numStations); 例如,我可以将其称为纯粹来自C,如: int testfunc() { stationMMnode_t *node1 = malloc(sizeof(struct stationMMnode)); node1->signalWindowLen = 500; stationMMnode_t *node2 = […]

将struct传递给函数

嘿我不确定为什么当我将一个Struct数组传递给一个函数时; 当我尝试访问它的成员时,它打印随机数。 语句“printf(”%d \ n“,netTopo [0] .nodes [1]);” 工作正常,但我在函数中尝试打印相同的数据,它打印一堆随机数? 不知道我做错了什么。 int main(int argc, char *argv[]) { if (argc != 3){ printf(“Incorrect command line arguments. Required 2 files.\n”); exit(-1); } FILE *netFile, *schFile; // put into a loop netFile = fopen(argv[1], “r”); schFile = fopen(argv[2], “r”); int *sched = getSchedFile(schFile); struct nodeInfo *netTopo = getTopology(netFile); printf(“%d\n”, […]

释放c – loop中的子串

我正在尝试为结构’ structs ‘的每个成员获取一个子字符串,然后将该子字符串分配给temp_struct的新成员。 我遇到的问题是如何在每次迭代时释放子字符串,由于某种原因代码运行,但是valgrind抛出一个Invalid read of size 1的Invalid read of size 1 ,我假设我正在读取内存块。 我怎么能释放子串呢? 谢谢 #include #include #include struct st_ex { char product[16]; float price; }; struct st_temp { char *prod; }; char *temp = NULL; // from stackoverflow char* substr( const char* source, size_t start, size_t end ) { char* dest = malloc( end […]

struct中的int字段没有初始化的值是多少?

在代码中:… #define MAXELEMNTS 100 struct book { int number; char name[31]; char author[31]; int year; char publisher[31]; }; struct book bkStruct[MAXELEMENTS]; … 在初始化其他char字段时,默认情况下整数字段(数字和年份)是否初始化为0但不是这两个? 或者他们有神知道什么价值? 根据我的经验,他们确实有值= 0,但我不确定这是一般规则所以我必须确定! 最好的问候,Papo

为什么short在C中的结构中存储为4个字节?

我有以下两种结构: 问题是sizeof(Content)返回160.结构由11个短路,6个整数,76个字符,7个浮点数,1个双精度组成,总共增加到158个字节。 我已计数三次,仍有2个字节的差异。 typedef struct TIME_T { short year,mon,day; short hour,min,sec; } TIME; typedef struct { int no; char name[20]; char Code[10]; char DASType[10]; short wlen; float VLtd; int samp; int comp; int locationID; short TranMode; char TranIns[12]; short TimerMode; char ClkType[12]; float ClkErr; float lat; float lon; float alt; float azimuth,incident; short weight; short […]

我应该如何malloc / realloc与包含数组的结构?

我对c很新,所以如果我的步骤有误,请告诉我。 假设我有以下内容: struct graphNode{ int val; graphNode* parent; int succSize; int succMaxSize; graphNode* succ[1]; }; 我将创建一个新节点: graphNode *n; n = malloc(sizeof(struct graphNode)); assert(n); n->val = 1; n->parent = NULL; n->succSize = 0; n->succMaxSize = 1; 然后,如果我想向节点添加后继 if (n->succSize == n->succMaxSize){ n->succ = realloc(n->succ, sizeof(graphNode*) * n->succMaxSize * 2); n->succMaxSize *= 2; } n->succ[succSize] = n2; […]

使用mmap共享结构数组

我正在尝试创建父进程和子进程之间共享的结构数组。 我试图访问arrays数据时遇到分段错误。 我确信这个问题与我使用指针的方式有关,因为这是一个我不太满意的领域。 请注意,我删除了大部分似乎不相关的代码。 /* structure of Registration Table */ struct registrationTable{ int port; char name[MAXNAME]; int req_no; }; main() { /* the registrationTable is to be a shared memory space with each child process able to access and update. No concurrency controls are implemented. The parent process is responsible for cleaning up after the […]