Driss Bounouar
Driss Bounouar

Reputation: 3242

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

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.

Upvotes: 0

Views: 14588

Answers (2)

vincentp
vincentp

Reputation: 1433

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.

Upvotes: 1

FunkyCat
FunkyCat

Reputation: 448

This works OK:

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

Upvotes: 6

Related Questions