0

Hello I'm trying to increase float to Pointer But somehow The program prints all the time 0.00000. The number should be drawn between 12.01 to -13.00.

My code -

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

int main()
{
    float* num = (float*)malloc(sizeof(float));

    srand(time(NULL));

    *num = rand() % 1300 + 1201 / 100.00;

    printf("%f",num);
    system("PAUSE");

    free(num);
}

I would love if someone could help me fix it thanks.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
user3332897
  • 23
  • 2
  • 8

6 Answers6

4

note the * if you want to print the number while you're printing the address :

printf("%f",*num);
jfly
  • 7,715
  • 3
  • 35
  • 65
3

You need to print the value that num points to:

printf("%f", *num);
perreal
  • 94,503
  • 21
  • 155
  • 181
1

Two things:

  1. Don't cast your malloc in C (unrelated to your problem but a good idea nonetheless)
  2. Enable compiler warnings

Doing both of these things will help you avoid problems. Compiling with gcc -Wall I get:

warning: format '%f' expects argument of type 'double', but argument 2 has type 'float *' [-Wformat=] printf("%f",num);

which would have answered your question. You are using a %f format specifier but passing a pointer, not a float or double. You need to dereference your pointer:

printf("%f", *num);
Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
1

These are errors:

  • *num = rand() % 1300 + 1201 / 100.00;

    *num is in the [12.01, 1311.01] range. If you need a number in the [12.01, 13.00] range, change the assignment:

    *num = 12.01 + (rand() % 100) / 100.0;
    
  • printf("%f", num); should be printf("%f", *num);

Also it's a good idea to enable extra warnings during compilation. E.g. with -Wall -Wextra

clang -Wall -Wextra filename.c
warning: format specifies type 'double' but the argument has type 'float *' [-Wformat]

Same with gcc

gcc -Wall -Wextra filename.c

warning: format '%f' expects argument of type 'double', but argument 2 has type 'float *' [-Wformat=]

If you are playing with malloc/free you should check for allocation failures.

Memory allocation is not guaranteed to succeed, and may instead return a null pointer. If there's no check for successful allocation implemented, this usually leads to a crash of the program, due to the resulting segmentation fault on the null pointer dereference.

So you could change your code in this way:

float *num = malloc(sizeof(float));
if (num)
{
  // your code
}
else
{
  // handle failure
}

Anyway it's better to just use a simple float.

manlio
  • 18,345
  • 14
  • 76
  • 126
1

In addition of fixing printf("%f", *num);, you need to check your math!

If you really want result between 12.01 and -13.00,

*num = (rand() % 2501 - 1300) / 100.00;
huocp
  • 3,898
  • 1
  • 17
  • 29
0

You program invokes undefined behaviour because num is not a float but a pointer to a float and your trying to print it as a float by %f conversion specifier in printf call. Also, don't cast the result of malloc and explicitly mention void in the paramater list of main.

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

int main(void) {
    float *num = malloc(sizeof *num);
    srand(time(NULL));
    *num = rand() % 1300 + 1201 / 100.00;
    printf("%f", *num);   // note *num instead of num
    system("PAUSE");

    free(num);
}
ajay
  • 9,402
  • 8
  • 44
  • 71