0

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.

7
  • 3
    This sounds like an XY problem. What are you trying to solve? Commented Nov 17, 2014 at 11:07
  • 1
    Use the code that "compiles alright". Commented Nov 17, 2014 at 11:07
  • I have a framework that uses a float* i can't pass a float[] so i can't use the code that compiles Commented Nov 17, 2014 at 11:08
  • 1
    floatTab will decay to a float* if used as a function parameter, but use the code that "complile [sic] alright" to create the array. Commented Nov 17, 2014 at 11:09
  • 1
    @DrissBounouar A pointer is not an array. That is the simple explanation for the error. That initialization is for arrays, not a pointer. A pointer is a single, integral value, not a set of values. Commented Nov 17, 2014 at 11:45

2 Answers 2

6

This works OK:

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I was doing some extra changes to the same above code trying to give a reference instead of doing as simple as this.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.