-1

I'm learning some c now, and must do an exercise for the university. I need an double array which should be filld with int values. So I've this.

#include <stdio.h>
#include <stdlib.h>

int main( int argc, char *argv[] ) {

int size=0, i=0;

printf("Enter size of array: ");
scanf("%d", &size);

double* field = malloc(size * sizeof(double));
double value;

for(i=0; i<size; i++) {
    field[i] = i;;
}

for(i=0; i<size; i++) {
    printf("Field%i: %d\n",i, field[i]);
}

free(field);

return 0;

}

But, when I execute it all values are set to 0.

Enter size of array: 10
Field0: 0
Field1: 0
Field2: 0
Field3: 0
Field4: 0
Field5: 0
Field6: 0
Field7: 0
Field8: 0
Field9: 0
jupper
  • 103
  • 2
  • 12
  • 1
    See [this Q&A](https://stackoverflow.com/q/2274336/335858) for an explanation. – Sergey Kalinichenko Dec 30 '17 at 10:46
  • 2
    Check the warning options for your compiler. Many compilers can issue a warning for format specifier mismatches - do not ignore warnings - especially do not ask questions about code that is issuing warnings - either ask about the warning specifically or fix the warnings before asking. – Clifford Dec 30 '17 at 11:14

2 Answers2

6

printf's %d stands for decimal integer, not double.

So this line:

printf("Field%i: %d\n",i, field[i]);

should be:

printf("Field%i: %f\n",i, field[i]);

Using the wrong format specifier is undefined behavior which you had earlier.

From standard

If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.


Also check the return value of malloc. It let's you know whether the allocation failed (returns NULL) or not.

A bit more insight from David C. Rankin's comment

The %d format specifier was looking for 4-bytes (sizeof int bytes) in memory ordered based on the endianess of your machine. When you provided an 8-byte (sizeof double) value comprised of a sign-bit, normalized exponent and mantissa, it had no idea what to do with it.

Well here in printf we can use %f instead of %lf also which has added benefit of compatibility with pre-C99 versions and also it is shorter. (chux pointed this).

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • The ole', I'm trying to print an IEEE-754 double-precision floating point value as an `int` and it's not working?? – David C. Rankin Dec 30 '17 at 10:54
  • Just understand, the `%d` *format specifier* was looking for 4-bytes in memory ordered based on the endianess of your machine. When you provided an 8-byte value comprised of a sign-bit, normalized exponent and mantissa, it had no idea what to do with it. – David C. Rankin Dec 30 '17 at 10:57
  • Sure, you have free license. Use at will. – David C. Rankin Dec 30 '17 at 11:01
  • @DavidC.Rankin.: Well I guess OP's machine is Little Endian and that's why it printed (which is not standard behavior) the 4 bytes of it which is `0`'s. (mantissa is 0's for all these numbers). `8.0`'s representation in hex is `0x4020000000000000`. – user2736738 Dec 30 '17 at 11:13
  • @DavidC.Rankin.: It's more correct to say that it is not not defined behavior rather than saying *it's not standard behavior*.. It may have crashed. – user2736738 Dec 30 '17 at 11:29
  • Why recommend `"%lf"` vs. the simpler `"%f"`? After all the [No-el](https://en.wikipedia.org/wiki/Noel) version is more in line with the season. – chux - Reinstate Monica Dec 30 '17 at 14:05
  • @chux.: Well it doesn't matter I guess. On the other hand any reason why you say should go with`%f`? :) :D . – user2736738 Dec 30 '17 at 14:08
  • 1) `"%f"` is shorter 2) `"%lf"` does not work with pre-C99 See [this](https://stackoverflow.com/q/4264127/2410359) and [that](https://stackoverflow.com/q/210590/2410359) – chux - Reinstate Monica Dec 30 '17 at 14:18
  • @chux.: I was aware of the `scanf` part but was misssing pre-C99 issue...Thanks as always – user2736738 Dec 30 '17 at 14:23
-1

I don't think whether malloc function allocated the memory for the array. Check the syntax of malloc and also the return type.