有关 scanf 和内存分配的帮助
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
float** getdata(void);
void calculations(float* fPtr);
//void printoriginal(float* values, int inputnum, float* fPtr);
int main(void)
{
float** fPtr;
float* values;
fPtr = getdata();
calculations(*fPtr);
int element;
// printoriginal(*values, inputnum, *fPtr);
system("PAUSE");
return 0;
}
float getvalues(void)
{
float* values = (float*)calloc(*inputnum, sizeof(float));
float** getdata(void)
{
int i;
int n;
float** fPtr;
int* inputnum;
printf("How many values do you want to input into the array?");
scanf("%d", inputnum);
float* values = (float*)calloc(*inputnum, sizeof(float));
if (values == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(i = 0; i < *inputnum; i++)
{
n = i + 1;
printf("Please enter your %d number: ", n);
scanf("%f",(values+i));
}
fPtr = (float**)calloc(*inputnum+1, sizeof(float*));
if (fPtr == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(int c=0; c < *inputnum; c++)
{
*(fPtr+i) = (values+i);
}
printf("%f", values+2);
return fPtr;
}
我扫描 getdata 中的值,但我正在扫描垃圾。我很确定我的 scanf 已关闭。另外,我如何能够通过引用传回我的值数组?我对此也感到非常困难。谢谢大家。
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
float** getdata(void);
void calculations(float* fPtr);
//void printoriginal(float* values, int inputnum, float* fPtr);
int main(void)
{
float** fPtr;
float* values;
fPtr = getdata();
calculations(*fPtr);
int element;
// printoriginal(*values, inputnum, *fPtr);
system("PAUSE");
return 0;
}
float getvalues(void)
{
float* values = (float*)calloc(*inputnum, sizeof(float));
float** getdata(void)
{
int i;
int n;
float** fPtr;
int* inputnum;
printf("How many values do you want to input into the array?");
scanf("%d", inputnum);
float* values = (float*)calloc(*inputnum, sizeof(float));
if (values == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(i = 0; i < *inputnum; i++)
{
n = i + 1;
printf("Please enter your %d number: ", n);
scanf("%f",(values+i));
}
fPtr = (float**)calloc(*inputnum+1, sizeof(float*));
if (fPtr == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(int c=0; c < *inputnum; c++)
{
*(fPtr+i) = (values+i);
}
printf("%f", values+2);
return fPtr;
}
I scanf the values in getdata, but I am scanning in garbage. Im pretty certain my scanf is off. Also, how would I be able to pass back my values array through reference?? I am having a very hard time with that as well. Thanks everyone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是
你想要的:
要么这样,要么你需要插入 inputnum: 的分配,
但这对于熟悉 C 的人来说看起来很奇怪
(除非有其他原因,在此
情况似乎没有任何原因。)
您遇到的问题是 scanf 是
将接收到的值存储在指向的位置
通过 inputnum,这是一个虚假位置,因为 inputnum
未初始化。
Instead of
you want:
Either that, or you need to insert an allocation of inputnum:
but that will look very strange to anyone familiar with C
(Unless there is some other reason for it, and in this
case there does not appear to be any reason.)
The problem you are experiencing is that the scanf is
storing the received value in the location pointed to
by inputnum, which is a bogus location since inputnum
is uninitialized.
我喜欢将对象“分配”给一个函数。在您的程序中,您的值在函数
getdata()
中创建,在calculations()
中使用,并且从未释放。在我看来,如果
main()
函数负责它们,那么编码会更容易。 在上面的代码片段中,我使用了函数
data_get()
和data_print()
,其原型与您的原型略有不同。这些函数采用一个指针和多个元素。第一个将最多
num
个双精度数读取到data
指向的内存中,并返回有效输入的双精度数。第二个需要打印双精度数。
哦!除非您有充分的理由使用浮动,否则不要使用它们。总是更喜欢
double
。I like to have objects "assigned" to one single function. In your program, your values are created in the function
getdata()
, used incalculations()
and never released.If the
main()
function was responsible for them, it would make coding easier, in my opinion. Something likeIn the snippet above, I used the functions
data_get()
anddata_print()
with a somewhat different prototype than you have. These functions take a pointer and a number of elements.The first one reads up to
num
doubles into the memory pointed to bydata
and returns the number of doubles that was effectively entered.The second takes number of doubles to print.
Oh! Unless you have a very good reason to use
float
s, don't use them. Always preferdouble
s.也可以代替
printf("%f",values+2);
使用
printf("%f", *(值+2));
Also Instead of
printf("%f", values+2);
use
printf("%f", *(values+2));
有很多错误,我不知道从哪里开始:)
下面完全重写了代码。请研究一下这个。
There is so many errors, I didn't know where to start :)
Code completely rewritten below. Please study this.