[英]Pointers in C using Malloc to allocate an array?
I'm having some problems with this excercise. 我在练习中遇到一些问题。 The way this program works is the user enters in the number of data sets so lets say 3. Then the user enters in the number of floats then the floats and then presses enter for the second and third data sets.
该程序的工作方式是用户输入数据集的数量,所以说3。然后,用户输入浮点数,然后是浮点数,然后按Enter键进入第二和第三数据集。 So an example input would be.
因此,将有一个示例输入。
Enter the number of data sets: 3 输入数据集数量:3
3 12.5 3.4 6.7 3 12.5 3.4 6.7
5 7.7 5.5 8.8 3.3 5.7 5 7.7 5.5 8.8 3.3 5.7
2 89.3 84.5 2 89.3 84.5
After that the user picks what data to perform an operation on let's say data 2 which is the [5 7.7 5.5 8.8 3.3 5.7] and then return the max of the numbers. 之后,用户选择要对[5 7.7 5.5 8.8 3.3 5.7]的数据2进行操作的数据,然后返回数字的最大值。
Since we don't know what the number of data sets we have to use a dynamically allocated array which I did in my code, and have pointers point to that array to find the max of that specific data set. 由于我们不知道数据集的数量,因此我们必须使用我在代码中所做的动态分配的数组,并且必须有指向该数组的指针来查找该特定数据集的最大值。 I think I need two arrays one to keep track of the data, and one to keep track of the sets.
我想我需要两个数组,一个用于跟踪数据,另一个用于跟踪集合。 Here is my code.
这是我的代码。
Im having trouble in the gettingData method I want the users inputs to be put in that array, but when I print what the pointer points to it returns 0.0000 which isn't accurate. 我在getingData方法中遇到麻烦,我希望将用户输入放入该数组中,但是当我打印指针指向它的内容时,它返回0.0000,这是不准确的。
You seem to forget one thing: before you attempt to read any variable in your code, it must be initialized. 您似乎忘记了一件事:在尝试读取代码中的任何变量之前,必须对其进行初始化。 Generally, initialization can be achieved by two kinds of instructions:
通常,初始化可以通过两种指令来实现:
a = expression
. a = expression
。 Note, that in order to assign a value to a
you must first obtain the value of expression
, so any variables used in expression
must be initialized first. a
分配值,您必须首先获取expression
的值,因此必须首先初始化expression
使用的任何变量。 &a
) of a variable to a function, which puts a value under the given address (which is done by writing *a = expr
). &a
)传递给函数,该函数将值放在给定的地址下(通过写*a = expr
)。 As before, expr
cannot use any uninitialized variables. expr
不能使用任何未初始化的变量。 What is more, your program does not seem to have any instruction which reads floats from input. 而且,您的程序似乎没有任何读取输入浮点数的指令。
What is more, remember to free
memory allocated by malloc
when you'r done with the data! 而且,记住在处理完数据后
free
由malloc
分配的malloc
! If you'r interested why, please read this thread. 如果您对为什么感兴趣,请阅读此主题。
As @Aubin said, you have passed not enough arguments to gettingData
function. 正如@Aubin所说,您没有将足够多的参数传递给
gettingData
函数。
Important note: When using a good compiler (ex. gcc
, clang
), it's always a good idea to use -Wall
flag, which spots simple mistakes like these, and warns you even before you have a chance to run the code. 重要说明:当使用良好的编译器(例如
gcc
, clang
)时,使用-Wall
标志始终是一个好主意,该标志可以发现诸如此类的简单错误,并在有机会运行代码之前发出警告。 Mistakes like those are done by programmers through their whole life, and -Wall
flag saves a lot of time to the whole mankind every day. 程序员一生都会
-Wall
这样的错误,并且-Wall
标志每天为整个人类节省大量时间。
I think we should not be coding for you here, but I can offer some advice for the code you shared (which have too many issues). 我认为我们不应该在这里为您编码,但是我可以为您共享的代码(有太多问题)提供一些建议。
It is a good practice to avoid writing any code until you have your logic straight. 最好避免在逻辑清晰之前编写任何代码。 A flowchart, sequence diagrams or any UML documents may be helpful.
流程图,顺序图或任何UML文档可能会有所帮助。 The idea is to invest time thinking on the most efficient way to get your goal, so then, coding can be the easy part.
想法是将时间花在最有效的方法上,以达到目标,因此,编码可以很容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.