问题描述:

In Python, I can initialize variables with itterables and automatic unpacking like in

a,b,c = [1,2,5]

In (openCL) C, can I do something similar, like so

uint4 vec = (uint4)(1,2,3,4);

uint a,b,c,d;

a,b,c,d = vec;

or do I have to use explicit vector components? Or is there any other good way to do this in a simple and correct way?

网友答案:

You need to assign values to each of the variables separately. You of course can write some specific function/macro do this task, but it's not worth it.

网友答案:

In C, the syntax for this is something like this:

yourType variablename[] = {yourdata};

You can even nest vectors if you are working with more than one variable

yourType variablename[][] = {{yourdata1},{yourdata2},...};

And you can use this even if you are trying to initialize a struct, but you must input the arguments in the same order as you declared on your struct

struct yourStruct {
type1 name1;
type2 name2;
type3 name3;
...
}

struct yourStruct variablename = {(type1) data1, (type2) data2, (type3) data3...};

And you can also nest a struct inside a struct, or a struct inside a vector, or a vector inside a struct, and so on and so forth...

Just be careful with pointers. They are not explicitly different from vectors, but should you try to initialize a pointer like a vector you will get a lot of nasty errors, and likely loose a few days trying to fix them.

相关阅读:
Top