Problem using calloc with array and returning a pointer

As a reference, this is the second part of my assignment:

int* generateFibonacci(int size);

This function will take an integer value called size. The value contained in the size variable will represent how many numbers in the Fibonacci sequence fit into the array. The function will use calloc

to create an array of this size, and then fill the array with numbers size

from the Fibonacci sequence starting with 1

and 1

. When the array is complete, the function will return a pointer to it.

My problem occurs when I get an error on line 8 " warning: assignment makes and integer from pointer without a cast

". The other error I am getting is on line 19 " warning: return makes pointer from integer without a cast

".

So my question is, how can I configure calloc

to make the array user-sized and 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;
}

      

-1


source to share


1 answer


How am I supposed to set up calloc to make a user-sized array and then return a pointer to it?

For one-dimensional array int * use printf()

andscanf()

int *array = {0}; //Note, leaving this initialization method for posterity 
                  //(See related comments below.)
                  //however agreeing with commentator that the more idiomatic
                  //way to initialize would be: int *array = NULL;
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 not clear from your example and comment if you are really going to use a 2D array ....

As pointed out in the comments, you created an array int

and then tried to create memory for it.

int i, array[size];   
...   
array[size]=(int*)calloc(size, sizeof(int));//wrong

      

array

No memory required during creation . The memory on the stack is created automatically.
If you wanted a 2D array int

. Then you can do it like this:

int  *array[size]; //create a pointer to int []  

      

With this, you can create an array of arrays (in concept) like this:

for(i=0;i<size;i++) array[i]= calloc(size, sizeof(int));//do not cast the output, not necessary  

      



You now have a 2D array size x size

int

. You can assign values ​​as follows:

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 operator parameters calloc()

as needed, but note that it is not necessary to render it to output.

As far as the return statement is concerned , your function is designed to return int *

.

int* generateFibonacci(int size){...} //requires a return of int *  

      

If you decide to use a one dimensional array, i.e. int *array={0}

(requiring memory allocation), return:

return array;//array is already a 'int *', just return it.

      

If you are using a 2D array to return int *

, you must decide which of the size

array elements you want to return:

return array[i];//where 'i' can be any index value, from 0 to size-1

      

+1


source







All Articles