在C中分配浮点指针 [英] Assigning Float Pointers in C
本文介绍了在C中分配浮点指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前拥有的是一个简单的函数,该函数基本上只是在给定参数指针的情况下重新分配指针,但是我收到一条错误消息,说我正在使用未初始化的变量之一.这就是我所拥有的,并且错误被抛出在* x = * k;
What I currently have is a simple function that basically just reassigns pointers given the parameter pointer but I'm getting an error saying that I'm using one of the variables uninitialized. Here's what I have and the error is being thrown on the line *x = *k;
float * program(const int *k)
{
float *x;
*x = *k;
return x;
}
这必须是一个非常简单的修复程序,但是我觉得我只是想念它.
This has got to be a really simple fix, but I feel like I'm just missing it.
推荐答案
以下是您的代码正在做的事情:
Here's what your code is doing:
float * program(const int *k)
{
// declare a pointer to a float, but don't initialize it,
// so it is not actually pointing anywhere specific yet.
float *x;
// Put the value pointed at by k into the location pointed at by x
// But wait - we don't know where x is pointing, so this is BAD!
*x = *k;
return x;
}
这就是为什么您的编译器抱怨的原因.
And that is why your compiler is complaining.
这篇关于在C中分配浮点指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文