简体   繁体   English

对指针进行类型转换?

[英]Typecasting an array to pointer?

Is is possible to typedef an array? 是否可以键入一个数组?

I have a set of vector function which all accept a pointer to a float which is an array of three floats. 我有一组向量函数,它都接受指向float的指针,float是一个由三个浮点数组成的数组。 I can typedef float* vec3_t, however it will not let me create an object by simply setting it equal to an array in brackets. 我可以使用typedef float * vec3_t,但它不会让我创建一个对象,只需将其设置为括号中的数组即可。

typedef float* vec3_t;

vec3_t a = {1,1,1}; // Does not work
vec3_t b = (float[]){1,1,1}; // Works
float c[] = {1,1,1}; // Works

void f(vec3_t x);

f({1,1,1}); // Error
f((float[]){1,1,1}; // OK

Could someone please explain why this works this way? 有人可以解释为什么这种方式有效吗?

A pointer and an array are not the same thing. 指针和数组不是一回事。 While they often behave in the same way, there are major differences, one of which you have just discovered. 虽然它们通常以相同的方式表现,但存在重大差异,其中一个是您刚刚发现的。

What your code actually does 你的代码实际上做了什么

I have substituted the typedefed type with real type, to make the explaination clearer. 我已经用类型替换了typedefed类型,以使解释更加清晰。

float c[] = {1,1,1};

You have simply created and initialized an array 您只需创建并初始化一个数组

f({1,1,1});

The code above is neither a lvalue nor rvalue. 上面的代码既不是左值也不是右值。 The {val1,...,valn} syntax is nothing more than an initializer and can not be used elsewehere. {val1,...,valn}语法只不过是初始化程序 ,不能在其他地方使用。

float* b = (float[]){1,1,1};

In here you have created and initialized an array and then stored it's location in a pointer. 在这里,您已创建并初始化了一个数组, 然后将其存储在指针中。

f((float[]){1,1,1};

This case is the same as the one above, but instead of storing the pointer you pass it as an argument to a function. 这种情况与上面的情况相同,但不是存储指针,而是将其作为参数传递给函数。

float* a = {1,1,1};

You are attempting to write three variables to a memory location that is not allocated yet. 您正在尝试将三个变量写入尚未分配的内存位置。 In general, {valn,...,valn} , is an initializer. 通常, {valn,...,valn}是初始化程序。 At this moment you have nothing to initialize. 此时你无需初始化。 Hence this syntax is invalid. 因此,此语法无效。 You are trying to pour gas into a canister that has not yet been manufactured. 您正在尝试将气体倒入尚未制造的罐中。

While I understand what you wanted to achieve, you seem to be missunderstanding the whole concept of memory and pointers. 虽然我理解你想要实现的目标,但你似乎错误地理解了内存和指针的整个概念。 Imagine this code, which (in some dirty logic) is an equivalent of what you're trying to do: 想象一下这段代码,它(在某些肮脏的逻辑中)相当于你想要做的事情:

float* a = NULL;
a[0] = 1;
a[1] = 1;
a[2] = 1;

What would happen if you executed this code? 如果执行此代码会发生什么?

So now you know why the compiler forbids it. 所以现在你知道为什么编译器会禁止它。

You have to many different features piled into your code, so it is not exactly clear what you mean by "why this works this way". 你必须将许多不同的功能堆积到你的代码中,所以你的意思并不完全清楚“为什么这样工作”。 What's "this" specifically? 什么是“这个”具体?

Anyway, in order to "typedef an array" you have to typedef an array, not pointer 无论如何,为了“typedef一个数组”你必须键入一个数组,而不是指针

typedef float vec3_t[3];

after which you will be able to do 之后你就能做到

vec3_t a = { 1, 1, 1 };

The rest of your code has nothing to do with typedefing an array. 其余代码与typedefing数组无关。 You simply discovered the compound literal syntax, which goes as (non-scalar-type) { initializers } and creates a nameless temporary object of the given type. 您只是发现了复合文字语法,它与(non-scalar-type) { initializers }并创建了给定类型的无名临时对象。 The (non-scalar-type) part is an important part of compound literal syntax. (non-scalar-type)部分是复合文字语法的重要部分。 You can't omit it. 你不能省略它。

So, instead of doing 所以,而不是做

vec3_t a = { 1, 1, 1 };
f(a);

if you don't care to have a named array object a , you can simply do 如果你不关心命名数组对象a ,你可以简单地做

f((vec3_t) { 1, 1, 1 });

or 要么

f((float [3]) { 1, 1, 1 });

or 要么

f((float []) { 1, 1, 1 });

with the same effect. 具有相同的效果。

An array is not a pointer. 数组不是指针。 An array has a base pointer. 数组一个基指针。 But anyway, the most applicable data structure for what you are trying to achieve would be something like: 但无论如何,您尝试实现的最适用的数据结构将是:

struct{
    float x;
    float y;
    float z;
} myFloat3;

Arrays are handy for variable length structures and stuff, but not the most efficient choice since you KNOW you have 3 floats and you can exploit that. 数组对于可变长度的结构和东西很方便,但不是最有效的选择,因为你知道你有3个浮点数,你可以利用它。 Lets the compiler pack lots of your structure efficiently, and allocate from the heap just what you need. 让编译器有效地打包您的大量结构,并从堆中分配您需要的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM