Tag: struct

传递struct的字段名称以访问函数内部

我有一个链表,我做了一个获取节点的function。 但我想用它来搜索名字或姓氏。 typedef struct people { char name[60], lastname[60]; struct people *next; } people; people *search(const char *key, people *list, FIELD) { while (list && strcmp(key, list->FIELD) != 0) { list = list->next; } return list; } 例: people *aux; aux = search(“John”, list_of_people, “name”); 要么: aux = search(“Smith”, list_of_people, “lastname”); 有一种清晰有效的方法来解决这个问题而不重复代码?

隐藏C结构定义

这是我的设置: 在public.h: #ifndef PUBLIC_H_ #define PUBLIC_H_ #include “func.h” /*extern typedef struct _my_private_struct PRIVATE_;*/ typedef struct _my_private_struct PRIVATE_; /* Thanks to larsmans and Simon Richter */ #endif 在struct.h中 #ifndef STRUCT_H_ #define STRUCT_H_ struct _my_private_struct { int i; }; #endif 在func.h中: #ifndef FUNC_H_ #define FUNC_H_ #include “struct.h” /* typedef struct _my_private_struct PRIVATE_; */ extern PRIVATE_ * get_new(int); […]

错误:在文件范围内修改了’d’

代码1: – int size; struct demo { int a; }; int main() { scanf(“%d”,&size); struct demo d[size]; return 0; } 这段代码工作正常。 代码2: – int size; struct demo { int a; }; int main() { scanf(“%d”,&size); return 0; } struct demo d[size]; 此代码显示错误: – error : variably modified ‘d’ at file scope 为什么这样的错误会出现在Code 2而Code 1运行正常?

使用C数组的哪种数据组织可以生成最快的代码?为什么?

根据以下数据,组织元素数组的最佳方法是什么,以便最快的随机访问? 每个元素都有一些int数,一个名称为3个字符,末尾带有’\ 0’,浮点值 。 我看到两种可能的方法来组织和访问这样的数组: 第一: typedef struct { int num; char name[4]; float val; } t_Element; t_Element array[900000000]; //random access: num = array[i].num; name = array[i].name; val = array[i].val; //sequential access: some_cycle: num = array[i].num i++; 第二: #define NUMS 0 #define NAMES 1 #define VALS 2 #define SIZE (VALS+1) int array[SIZE][900000000]; //random access: num […]

scanf到struct不起作用

我有这个function,我需要获得一个结构的坐标。 这些是结构: // ——— ———-结构 typedef struct coordinates { int x_l; int y_l; int x_r; int y_r; } Coordinates; typedef struct field { char Id; Coordinates location; int area; int price; } Field; 这是function: Field GetFieldFromUser(int cptr,Field *pf1) { //Field *pf int i=0; printf(“\nPlease enter information for your new field:\n”); printf(“the Id of the field […]

通过Emscripten在Javascript中进行结构化操作

我在使用C和Javascript之间的emscripten互操作方面遇到了很多问题。 更具体地说,我无法访问在javascript中使用C创建的结构,因为指向结构的指针作为外部库传递给javascript。 看一下下面的代码: C: #include #include #include struct test_st; extern void read_struct(struct test_st *mys, int siz); struct test_st{ uint32_t my_number; uint8_t my_char_array[32]; }; int main(){ struct test_st *teststr = malloc(sizeof(struct test_st)); teststr->my_number = 500; for(int i = 0; i my_char_array[i] = 120 + i; } for(int i = 0; i my_char_array[i]); } read_struct(teststr,sizeof(teststr)); return 0; […]

在Cython中返回一个结构数组

我试图在Cython中返回一个结构数组。 // .pyx from libc.stdint cimport uint8_t cdef extern from “”: cdef struct apriltag_detection: int id double c[2] double p[4][2] ctypedef apriltag_detection apriltag_detection_t cdef extern from “tag36h11_detector/tag36h11_detector.h”: apriltag_detection_t* scan_frame(int width, int height, uint8_t* data); cdef class Detection: # how do I “link” this to the struct defined above? def __cinit__(self): pass def __dealloc__(self): pass def […]

如何在C中转储任意结构?

我不知道要去哪个方向,也许reflection会有所帮助?

结构数组 – 删除/添加元素和打印

对于以下代码 struct orderSlip { char source[64]; char destination[64]; char item[64]; int position; }; //global struct orderSlip data[100]; 除了以下方法之外,还有其他方法可以打印出每个元素的数据: printf(“%s\n”,data[0].source); printf(“%s\n”,data[0].destination); printf(“%s\n”,data[0].item); printf(“%i\n\n”, data[0].position); printf(“%s\n”,data[1].source); printf(“%s\n”,data[1].destination); printf(“%s\n”,data[1].item); printf(“%i\n”, data[1].position); 等等 for(int n = 0; n< 3; n++) { printf("%s\n",data[n].source); printf("%s\n",data[n].destination); printf("%s\n",data[n].item); printf("%i\n\n", data[n].position); } 要删除和添加,我是否必须创建一个动态的结构数组? 如果是这样,最简单的语法是什么? 像这个c ++代码的东西 int * bobby; bobby = new int [5]; […]

这个位域会以我期望的方式工作吗?

我一直在读C中的位域,C标准如何不强制机器字中字段的任何特定顺序,等等。 我希望这个问题适合SO的格式。 我的问题是我的结构(后面的定义)是否会以我期望的方式实际执行。 这是我想出的定义,然后我会讨论我想要的东西: typedef enum { STATE_ONE, STATE_TWO, STATE_THREE, STATE_FOUR } __attribute__ ((packed)) State; typedef struct MyStruct { // One of State enum (maximum of 4 states). unsigned state : 2; // Remaining 30 bits are used differently depending on ‘state’. union { // If ‘state’ is STATE_ONE (0), the remaining bits are an […]