麻烦用释放calloc一个数组,并返回一个指针 [英] Trouble with using calloc with an array and returning a pointer
问题描述
作为参考,这是我的任务的第二部分:
为int * generateFibonacci(INT大小);
此功能将作为输入称为大小的整数。中包含的尺寸变量值
将重新present在Fibonacci序列中有多少个数字来放入数组。功能
将使用释放calloc
来创建这个大小的数组,然后填充从尺寸
数字数组
斐波那契序列,从1
和1
。当数组完成后,函数将返回
指针。
块引用>我的麻烦进来玩,当我在第8行错误
警告:赋值时和指针整数,未作投
。
我得到的另一个错误是在第19行警告:返回时将整数指针,未作投
所以我的问题是,我怎么想建立
释放calloc
来做出一个尺寸阵列从一个用户,然后返回一个指向它?的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;为int * generateFibonacci(INT大小)
{
INT I,阵列【尺寸】; 数组[大小] =(INT *)释放calloc(大小,sizeof的(INT)); 数组[0] = 0;
阵列[1] = 1; 对于(i = 2; I<尺寸+ 1;我++) 阵列[I] =阵列〔Ⅰ-2〕+阵列[I-1]; 返回数组*;
}无效printHistogram(int数组[],INT大小)
{
INT I,J; 对于(i = 0; I< =大小; ++ I)
{
为(J = 0; J<阵列[我]; J ++)
{
的printf(*);
}
的printf(\\ n);
}
}INT主要(无效)
{
int数组[100],大小; 的printf(有多大你会数Fibionacci呢?);
scanf函数(%i的,&安培;大小); generateFibonacci(大小);
printHistogram(数组大小); 返回0;
}
解决方案的我怎么想成立释放calloc使来自用户的尺寸数组,然后返回一个指向它?的
有关为int的一维数组*使用
的printf()
和scanf()的
为int *阵列= {0};
为size_t大小= 0;
的printf(请输入数组的顺序);
scanf函数(%d个,&安培;大小);
阵列=的malloc(大小); //创建为大小的元素存储空间
如果(阵列){//做其他的东西}但是的它是从你的榜样不清楚,和评论,如果你真的打算使用一个二维数组....
正如评论所说,你已经创建了一个
INT
磁盘阵列,然后试图为它创建的内存。INT I,阵列【尺寸】;
...
数组[大小] =(INT *)释放calloc(大小,sizeof的(INT)); //错误由于它是创建
阵列
不需要记忆。内存堆栈自动创建。结果
如果你想INT
的二维数组。然后,你可以做这样的:为int *阵列【尺寸】; //创建一个指针为int []
通过这个,你可以用这种方式创建数组的数组(概念):
为(i = 0; I<大小;我++)阵列[我] =释放calloc(大小,sizeof的(INT)); //不投的输出,没有必要
现在,你必须要有一个
尺寸X尺寸
二维数组。它可以以这种方式被分配的值:INT
的为(i = 0; I<大小;我++)
为(J = 0; J<大小; J ++)
数组[I] [J] = I *焦耳; //或一些有用的分配另外,需要调整
释放calloc的参数()
语句, 但要注意的,铸造它的输出是没有必要的。关于return语句的后,你的函数的原型返回一个
为int *
。为int * generateFibonacci(INT大小){...} //需要为int的回报*
如果您决定使用一维数组,即
为int *阵列= {0}
(要求您分配内存),则返回:返回的数组; //阵列已经是一个`INT *`,刚刚返回。
如果您使用的是二维数组,然后返回一个
为int *
,你必须决定哪些尺寸
数组元素要返回:返回的数组[我]; //其中`i`可以是任何索引值,从0到大小-1
As a reference this is the second part of my assignment:
int* generateFibonacci(int size);
This function will take as input an integer called size. The value contained in the size variable will represent how many numbers in the Fibonacci sequence to put into the array. The function will use
calloc
to create the array of this size and then fill the array withsize
numbers from the Fibonacci sequence, starting with1
and1
. When the array is complete the function will return a pointer to it.My trouble come in play when I get the error in line 8 "
warning: assignment makes and integer from pointer without a cast
". Another error I get is in line 19 "warning: return makes pointer from integer without a cast
".So my question is, how am I suppose to set up
calloc
to make the array with a size from a user, then return a pointer to it?#include <stdio.h> #include <stdlib.h> int* generateFibonacci(int size) { int i, array[size]; array[size]=(int*)calloc(size, sizeof(int)); array[0]=0; array[1]=1; for(i = 2; i < size+1; i++) array[i] = array[i-2] + array[i-1]; return *array; } void printHistogram (int array[], int size) { int i, j; for(i=0; i <= size; ++i) { for(j=0; j < array[i]; j++) { printf("*"); } printf("\n"); } } int main(void) { int array[100], size; printf("how big will your Fibionacci number be? "); scanf("%i", &size); generateFibonacci(size); printHistogram(array, size); return 0; }
解决方案how am I suppose to set up calloc to make the array with a size from a user, then return a pointer to it?
For a 1D array of int * Use
printf()
andscanf()
int *array = {0}; size_t size = 0; printf("Enter order of array"); scanf("%d", &size); array = malloc(size);//create memory with space for "size" elements if(array){//do other stuff}
But it is unclear from your example, and the comment if you really intend using a 2D array....
As stated in the comments, You have created an
int
array, then attempted to create memory for it.int i, array[size]; ... array[size]=(int*)calloc(size, sizeof(int));//wrong
As it is created,
array
does not need memory. Memory is created on the stack as automatic.
If you wanted a 2D array ofint
. Then you could do it like this:int *array[size]; //create a pointer to int []
With this, you can create an array of arrays (in concept) in this way:
for(i=0;i<size;i++) array[i]= calloc(size, sizeof(int));//do not cast the output, not necessary
Now, you essentially have a
size x size
2D array ofint
. It can be assigned values in this manner:for(i=0;i<size;i++) for(j=0;j<size;j++) array[i][j]=i*j;//or some more useful assignment
By the way, adjust the parameters of the
calloc()
statement as needed, but note, casting its output is not necessary.Regarding the return statement, your function is prototyped to return a
int *
.int* generateFibonacci(int size){...} //requires a return of int *
If you decide to use a 1D array, i.e.
int *array={0}
(requiring that you allocate memory), then return:return array;//array is already a `int *`, just return it.
If you are using the 2D array, then to return a
int *
, you must decide which of thesize
elements of the array you want to return:return array[i];//where `i` can be any index value, from 0 to size-1
这篇关于麻烦用释放calloc一个数组,并返回一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!