我可以像对待数组一样对待结构吗? [英] Can I treat a struct like an array?

查看:26
本文介绍了我可以像对待数组一样对待结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于保存 4D 矢量的结构

I have a struct for holding a 4D vector

struct {
    float x;
    float y;
    float z;
    float w;
} vector4f

而且我正在使用一个库,该库具有一些对向量进行操作但将浮点指针作为其参数的函数.

And I'm using a library that has some functions that operate on vectors but take float pointers as their arguments.

调用诸如 doSomethingWithVectors( (float *) &myVector) 之类的东西是否合法?

Is it legal to call something like doSomethingWithVectors( (float *) &myVector)?

推荐答案

它可能有效,但不可移植,编译器可以自由对齐内容,因此一个浮点数不必立即跟随另一个浮点数.

It might work but it is not portable, the compiler is free to align things so that one float does not neccessarily immediately follow the other.

这篇关于我可以像对待数组一样对待结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆