C++ convert a float array to float* (pointer)

13.9k Views Asked by At

I wonder how to convert a float array to a float* I have this situation :

float* floatTab = {12f, 0.5f, 3f};

It gives me an error here. but if I write it like this float floatTab[3] = {12f, 0.5f, 3f};it compiles alright.

2

There are 2 best solutions below

1
On BEST ANSWER

This works OK:

float floatTab[3] = {12f, 0.5f, 3f}; float* ptr = floatTab;

0
On

Prefer STL containers instead of C arrays (or others RAII-conform classes):

const std::array<float, 3> array = { 1.f, 2.f, 3.f };
float *ptr = &array[0];

Don't forget to include <array> and <initializer_list> to compile this code.