Tag: casting

将char数组转换为struct *类型

在下面的代码中,有人可能会解释行struct ether_header *eh = (struct ether_header *) sendbuf;上发生了什么struct ether_header *eh = (struct ether_header *) sendbuf; ? 我知道它正在创建一个类型为ether_header的指针eh ,并且在RHS上,您正在将sendbuf转换为tyoe struct ether_header的指针。 但是你怎么能这样做是sendbuf是一个char array ? 你也为什么要这样做? 这是完整代码发送以太网帧的链接 #include #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int sockfd; struct ifreq if_idx; struct ifreq if_mac; int tx_len = 0; char sendbuf[BUF_SIZ]; struct […]

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

我有一个包含各种兼容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)); […]

防止使用整数进行自动转换

我正在开发一个音频应用程序(用C ++编写),我有很多相互调用的函数,它们可以采用帧数(即1个单声道或2个立体声浮点样本)或原始数量的样本…… 跟踪每个函数(样本或框架?)的语义以及何时通过nChannels跟踪多个或div,所以我想以某种方式做一个typedef samples_t和frames_t(到unsigned int)并让编译器帮助变得越来越困难我出去…… 是否有任何简单的方法可以将从frames_t到samples_t的隐式转换在C ++中标记为错误?

如何将C ++回调传递给C库函数?

我正在使用C ++开发我的代码,并希望使用MPFIT非线性曲线拟合库,它是用C开发的,但允许用C ++编译。 例如,我有一个名为“myClass”的类,这个类有一个函数myClass :: Execute() 我在myClass.h文件中包含“mpfit.h”。 并尝试从Execute()调用一个名为mpfit的函数。 int status = mpfit(ErrorFunction, num1, num2, xsub_1D, 0, 0, (void *) &variables, &result); 问题是ErrorFunction是myClass的一个function。 所以编译器在我尝试使用它时会出错。 我试图从类对象中携带Erro​​rFunction,但这次我采取下面给出的错误: ErrorFunction在类之外时出错: 错误4错误C2664:’mpfit’:无法将参数1从’int(__ cdecl *)(int,int,double *,double,double *,void *)’转换为’mp_func’ ErrorFunction在类中时出错: Error 3 error C3867: ‘myClass::ErrorFunction’: function call missing argument list; use ‘&myClass::ErrorFunction’ to 错误函数的定义: int ErrorFunction(int dummy1, int dummy2, double* xsub, double *diff, […]

void *是字面上的浮点数,如何投射?

所以我在我的C ++应用程序中使用这个C库,其中一个函数返回一个void *。 现在我对纯C来说并不是最敏锐的,但是听说过一个void *可以被投射到几乎任何其他的*类型。 我也知道我希望在这个函数的某个地方有一个浮点数。 所以我将void *转换为float *并取消引用float *,崩溃。 该死的! 我调试代码并在gdb中让它评估(float)voidPtr和low并且看到值是我期望和需要的! 但是等等,在编译期间不可能一样。 如果我写float number = (float)voidPtr; 它不编译,这是可以理解的。 所以现在的问题是,如何让我的浮动脱离这个虚弱的空虚*? 编辑:感谢H2CO3这已经解决了,但我看到很多答案和评论出现并且不再相信我可以在gdb中做(浮动)voidPtr。 这是截图。

将Cython中的C结构包装/转换为Python类

我刚刚开始熟悉Cython,试图将一些C语言结构包装到Python方法和类中。 我真正不了解的是如何从(初始化的)C结构转换到相应的Python类应该是有效的。 我在这里缺少什么: 来自C头文件的片段: struct test_struct { int _something; complex_struct* _another; }; typedef struct test_struct test; test *test_new(void); int some_method(test **n, int irrelevant); 来自我.pxd的相应片段: cdef struct test_struct: pass ctypedef test_struct test test* test_new() int some_method(test **n, int irrelevant) 我的.pyx: def do_something(int irrelevant): cdef test* t = test_new() ret = some_method(&t, irrelevant) # Here comes the […]

具有相同成员类型的C结构是否保证在内存中具有相同的布局?

基本上,如果我有 typedef struct { int x; int y; } A; typedef struct { int h; int k; } B; 我有A a ,C标准是否保证((B*)&a)->k与ay相同?

从char **隐式转换为const char **

为什么我的编译器(GCC)没有从char**隐式转换为const char** ? 以下代码: #include void print(const char** thing) { std::cout << thing[0] << std::endl; } int main(int argc, char** argv) { print(argv); } 给出以下错误: oi.cpp: In function ‘int main(int, char**)’: oi.cpp:8:12: error: invalid conversion from ‘char**’ to ‘const char**’ [-fpermissive] oi.cpp:3:6: error: initializing argument 1 of ‘void print(const char**)’ [-fpermissive]

将一个结构指针转换为另一个 – C

请考虑以下代码。 enum type {CONS, ATOM, FUNC, LAMBDA}; typedef struct{ enum type type; } object; typedef struct { enum type type; object *car; object *cdr; } cons_object; object *cons (object *first, object *second) { cons_object *ptr = (cons_object *) malloc (sizeof (cons_object)); ptr->type = CONS; ptr->car = first; ptr->cdr = second; return (object *) ptr; } […]