Tag: struct

C Struct编译错误

为什么以下代码会产生编译时错误? 我似乎无法理解为什么类型不匹配。 typedef char f_string[MAX_CHARS+1] ; /* string for each field */ /* * A parsed CSV line, with the number of fields and upto MAX_FIELDS themselves. */ typedef struct { int nfields ; /* 0 => end of file */ f_string field[MAX_FIELDS] ; /* array of strings for fields */ } csv_line; …. csv_line […]

将结构指针转换为联合指针

我有一个包含各种兼容struct成员的union : enum Type { T1, T2, … }; struct S1 { enum Type type; … }; struct S2 { enum Type type; … }; … union U { enum Type type; struct S1 as_s1; struct S2 as_s2; … }; 这些结构的type字段被视为不可变,并且只访问与当前type对应的union的字段。 我知道只要type不变量得到维护,就会定义将union U *为struct S1 * ,但反过来也是如此吗? struct S1 *s1 = malloc (sizeof (struct S1)); […]

带指针结构的SWIG函数

我使用SWIG来包装C共享库。 我有问题在python中使用struct指针调用C函数。 我的文件: ST_Param.h: typedef struct { unsigned int* device_Address; …. …. unsigned int lock; }ST_param_ST; unsigned char ST_Param_Initialize(ST_param_ST * ST_param, unsigned int device_Address); ST_Param.c ……… Rest of file…………. unsigned char ST_Param_Initialize(ST_param_ST * ST_param, unsigned int device_Address){ if(ST_param == NULL){ ………. rest of funtion ………………….. return 0; } 在ST_Param_Initialize中我确认指针存在,如果不相信 ST_Param.i : /* File : ST_Param.i […]

C vs C ++中的typedef和struct命名空间

我试图在一些新的C ++中使用一些旧的C库。 该库的头文件使用D. Hanson的“C接口和实现”实现隐藏成语: #define T MyAST typedef struct T *T; 接近我可以说,这用C编译,因为在C中,struct struct和typedef名称在不同的名称空间中但是它不能用C ++编译( extern “C” { #include “MyAST.h” } ),因为typedef和struct names在同一名称空间中。 conflicting declaration ‘typedef struct MyAST* MyAST’ 我想我注定要将struct def移到标题中并放弃使用该技术,但我真的不想(这个成语用在很多代码中,有些是我的,有些不是)并认为我是在这里查看是否有人有任何见解。 PS:如果你不知道这个成语,它可以让你将结构定义保存在实现C文件中,然后接口的用户( MyAST.h )无法访问结构,他们必须在实现中使用你的函数。

C / C ++ Struct内存布局等效

考虑以下C结构和C ++结构声明: extern “C” { // if this matters typedef struct Rect1 { int x, y; int w, h; } Rect1; } struct Vector { int x; int y; } struct Rect2 { Vector pos; Vector size; } Rect1和Rect2对象的内存布局是否始终相同? 具体来说,我可以安全地从Rect2*到Rect1*并假设Rect2对象中的所有四个int值Rect2匹配到Rect1的四个int吗? 如果我将Rect2更改为非POD类型(例如通过添加构造函数)会有所不同吗?

apcs-gnu ABI中的结构布局

对于此代码 : struct S { unsigned char ch[2]; }; int main(void) { _Static_assert( sizeof(struct S) == 2, “size was not 2”); } 使用带有ABI apcs-gnu (又名OABI或EABI版本0)的ARM的GCC(各种版本),我得到断言失败。 事实certificate结构的大小是4 。 我可以通过使用__attribute__((packed))解决这个问题; 但我的问题是: 使这个结构大小为4的理由是什么? 是否有任何文档指定此ABI中结构的布局? 在ARM网站上,我找到了aapcs (EABI第5版)的文档,它确实将此结构指定为大小为2; 但我找不到关于apcs-gnu任何信息。

在struct中访问数组元素时出错

我正在尝试编写一个“ArrayList”程序(类似于Java ArrayList ),它将使用realloc自动扩展,这样程序员就不必担心数组中的存储空间。 这是我的代码: #include #include #include #include #define ARR_DEFAULT_SIZE 20 #define INCR 10 #define ARRTYPE char // Files using this #undef this macro and provide their own type typedef struct { ARRTYPE *arr; long size; long nextincr; } arrlst; arrlst *nlst(void); void add(arrlst *, ARRTYPE); ARRTYPE elmat(arrlst *, long); int main(int argc, char **argv) […]

Python Ctypes传入指针并获得结构

这是我在解决实际有用问题之前尝试工作的一个简单示例。 C代码: typedef struct { uint32_t seconds; uint32_t nanoseconds; } geoTime; int myTest(geoTime *myTime){ printf(“Time: %d %d\n”, myTime->seconds, myTime->nanoseconds); myTime->seconds = myTime->nanoseconds; geoTime T = {314, 159}; printf(“MyTime: %d %d retValue: %d %d\n”, myTime->seconds, myTime->nanoseconds, T.seconds, T.nanoseconds); return 314; } Python代码: import ctypes import time import math lib_astro = ctypes.CDLL(“libastroC.so”) class geoTime(ctypes.Structure): _fields_ = [(“seconds”, […]

结构数组的qsort不起作用

我试图通过char对结构运行数组进行排序,但是当我打印数组时,没有任何内容被排序。 看看这个: struct run { char name[20], weekday[4], month[10]; (And some more…) }; typedef struct run run; int name_compare(const void *a, const void *b) { run *run1 = *(run **)a; run *run2 = *(run **)b; return strcmp(run1->name, run2->name); } int count_number_of_different_persons(run results[]) { int i = 0; qsort(results, sizeof(results) / sizeof(run), sizeof(run), name_compare); for(i = […]

使用C中的另一个变量命名变量

我想创建一个包含2个变量的结构,例如 struct myStruct { char charVar; int intVar; }; 我将这些结构命名为: struct myStruct name1; struct myStruct name2; 等等 问题是,我不知道将输入多少变量,因此必须有无限的nameX结构。 那么,我如何用变量命名这些结构呢? 谢谢。