简体   繁体   English

随机和指针不起作用

[英]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: 两件事情:

  1. Don't cast your malloc in C (unrelated to your problem but a good idea nonetheless) 不要将您的malloc为C语言(与您的问题无关,但还是个好主意
  2. Enable compiler warnings 启用编译器警告

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格式说明符,但传递的是指针,而不是floatdouble 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.

相关问题 为什么 strsep() 不能使用指向堆栈的指针? - Why doesn't strsep() work with pointers to the stack? 带指针的C函数在一台计算机上工作,而在另一台计算机上不工作 - C function with pointers work on one computer, and doesn't work on another 为什么两个char指针之间的字符串复制不起作用? - Why doesn't string copy between two char pointers work? 我在 C 中的随机字符串生成器不起作用 - My random string generator in C doesn't work 为什么这段代码有效,而另一段无效? (指针和链接列表) - Why does this piece of code work and the other doesn't? (pointers and linked lists) 即使两个指针都指向同一个地址,为什么以下命令也不起作用? - Why doesn't the following work even though they both pointers point to the same address? c:当使用指针作为函数的输入时,通过使用* pointer ++递增指针值不起作用 - c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work 使用带有结构变量的指针的递归函数中的语法。 它不起作用,我认为主要问题是语法 - Syntax in recursive functions using pointers with struct variables. It doesn't work, and I think the main problem is the syntax Qsort不会对指向字符串的指针数组进行排序 - Qsort doesn't sort array of pointers to strings memcpy失败,但是赋值不指向字符指针 - memcpy fails but assignment doesn't on character pointers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM