[英]random and pointers doesn't work
Hello I'm trying to increase float to Pointer But somehow The program prints all the time 0.00000. 您好,我试图将浮点数增加到Pointer,但是不知何故,程序始终打印0.00000。 The number should be drawn between 12.01 to -13.00. 该数字应介于12.01到-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. 如果有人可以帮助我解决该问题,我将非常感谢。
如果要在打印地址时打印号码,请注意*:
printf("%f",*num);
您需要打印num指向的值:
printf("%f", *num);
Two things: 两件事情:
malloc
in C (unrelated to your problem but a good idea nonetheless) 不要将您的malloc
为C语言(与您的问题无关,但还是个好主意 ) Doing both of these things will help you avoid problems. 这两件事都可以帮助您避免问题。 Compiling with gcc -Wall
I get: 用gcc -Wall
编译gcc -Wall
我得到:
warning: format '%f' expects argument of type 'double', but argument 2 has type 'float *' [-Wformat=] printf("%f",num); 警告:格式'%f'期望类型为'double'的参数,但是参数2的类型为'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
. 您正在使用%f
格式说明符,但传递的是指针,而不是float
或double
。 You need to dereference your pointer: 您需要取消引用指针:
printf("%f", *num);
These are errors: 这些是错误:
*num = rand() % 1300 + 1201 / 100.00;
*num
is in the [12.01, 1311.01] range. *num
在[12.01,1311.01]范围内。 If you need a number in the [12.01, 13.00] range, change the assignment: 如果需要[12.01,13.00]范围内的数字,请更改分配:
*num = 12.01 + (rand() % 100) / 100.0;
printf("%f", num);
should be printf("%f", *num);
应该是printf("%f", *num);
Also it's a good idea to enable extra warnings during compilation. 另外,在编译过程中启用额外的警告也是一个好主意。 Eg with -Wall -Wextra
例如-Wall -Wextra
clang -Wall -Wextra filename.c
warning: format specifies type 'double' but the argument has type 'float *' [-Wformat]
Same with gcc 与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. 如果您正在使用malloc
/ free
,则应检查分配失败。
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. 无论如何,最好只使用一个简单的浮点数。
In addition of fixing printf("%f", *num);
除了修复printf("%f", *num);
, you need to check your math! ,您需要检查一下数学!
If you really want result between 12.01 and -13.00, 如果您确实希望结果在12.01到-13.00之间,
*num = (rand() % 2501 - 1300) / 100.00;
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. 你程序调用未定义的行为,因为num
是不是float
,但一个指向float
和你试图打印它作为一个float
的%f
转换说明在printf
调用。 Also, don't cast the result of malloc
and explicitly mention void
in the paramater list of main
. 另外,不要转换malloc
的结果,而在main
的参数列表中明确提及void
。
#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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.