0

I'm trying this simple example to populate a floating point array with 5.6, but upon printing the values out, every value is just 0.0.

#include <string.h>
float testArr[20];
memset(testArr, (float)5.6, 3*sizeof(float));
printf("Value 1: %lf\n",testArr[0]);
printf("Value 2: %lf\n",testArr[1]);
printf("Value 3: %lf\n",testArr[2]);
printf("Value 4: %lf\n",testArr[3]);

I've also tried not casting 5.6 as a float, setting testArr[20] = {} and testArr[20] = {0}, but they also result in the same 0.0.

4
  • 4
    Read the declaration of memset() and then search for floating-point to integer conversion. None of them are magic. Commented Oct 26, 2013 at 5:50
  • You need to use a for loop, not memset Commented Oct 26, 2013 at 5:50
  • 1
    memset converts the second parameter to a char. See memset - C++ Reference. Commented Oct 26, 2013 at 5:50
  • Does this code compile without a warning? Commented Oct 26, 2013 at 6:10

1 Answer 1

4

memset fills the memory with 1 char, not float.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.