继承我的代码:
#include <stdio.h>
#include <CL/cl.h>
#include <CL/cl_platform.h>
int main(){
cl_float3 f3 = (cl_float3){1, 1, 1};
cl_float3 f31 = (cl_float3) {2, 2, 2};
cl_float3 f32 = (cl_float3) {2, 2, 2};
f3 = f31 + f32;
printf("%g %g %g \n", f3.x, f3.y, f3.z);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用gcc 4.6进行编译时,会产生错误
test.c:14:11: error: invalid operands to binary + (have ‘cl_float3’ and ‘cl_float3’)
Run Code Online (Sandbox Code Playgroud)
对我来说很奇怪,因为OpenCL规范在6.4节中只是增加了两个floatn.我是否需要包含任何其他标题?
但更奇怪的是,在编译时-std=c99我得到的错误就像
test.c:16:26: error: ‘cl_float3’ has no member named ‘x’
Run Code Online (Sandbox Code Playgroud)
..对于所有组件(x,y和z)......
例如,联合的默认对齐方式如下:
union{
uint32_t v4;
__uint128_t v6;
}ip;
//in memory
//aaaa
//bbbbbbbbbbbbbbbb
Run Code Online (Sandbox Code Playgroud)
但我想让工会正确对齐:
// aaaa
//bbbbbbbbbbbbbbbb
Run Code Online (Sandbox Code Playgroud)
是否有可能在 C 中实现这一点?
我知道我们可以访问匿名联盟而无需创建它的对象(没有点),但任何人都可以解释一下,匿名联盟在现实世界的c ++编程中有什么用?
我看到了在C11 中用匿名方法实现某种struct继承的方法struct,并想尝试一下。这是我所拥有的:
struct struct_a {
int aa;
};
struct struct_b {
struct struct_a;
int bb;
};
int main(void)
{
volatile struct struct_b my_b;
my_b.aa = 5; /* not a member of my_b */
my_b.bb = 6;
}
Run Code Online (Sandbox Code Playgroud)
来自gcc的结果:
$ gcc -std=c11 struct_extend.c
struct_extend.c:11:20: warning: declaration does not declare anything
struct struct_a;
^
struct_extend.c: In function ‘main’:
struct_extend.c:18:9: error: ‘volatile struct struct_b’ has no member named ‘aa’
my_b.aa = 5; /* not a member of …Run Code Online (Sandbox Code Playgroud) 我目前使用三个不同的函数来返回一个数值(一个返回一个double,另外两个返回一个long):
int main(void)
{
// lots of code
dRate = funcGetInterestRate();
lMonths = funcGetTerm();
lPrincipal = funcGetPrincipal();
// lots of code
return 0;
}
Run Code Online (Sandbox Code Playgroud)
三个函数代码大约相同,所以我想合并为1个函数.我想将值标志传递给单个函数,如下所示:
doublelonglong我只想在调用函数时从函数返回1个值,但我想要返回的值可以是a double或a long.我想做这样的事情:
void funcFunction(value passed to determine either long or double)
{
// lots of code
if (foo)
return double value;
else
return long value;
}
Run Code Online (Sandbox Code Playgroud)
是否有捷径可寻?
如何在 C 中调用采用匿名结构的函数?
比如这个函数
void func(struct { int x; } p)
{
printf("%i\n", p.x);
}
Run Code Online (Sandbox Code Playgroud) 在C语言中,struct Animal;第6行的含义是什么?
在C89或C99或C11中是否合法?
struct Animal {
char *name;
int age;
};
struct Cat {
struct Animal; // line 6
int category;
};
Run Code Online (Sandbox Code Playgroud)
谢谢!