-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 with size numbers from the Fibonacci sequence, starting with 1 and 1. 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;
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    You're using *both* a variable-length array *and* `calloc`. Instead, define `array` as `int *array;`. And you shouldn't bother casting the return of `calloc`. And you should probably be using `%d` in your `scanf` (unless you really want to accept the input as octal if it begins with a `0`). – ooga Nov 12 '14 at 19:20
  • 1
    `int *array = calloc(size+1, sizeof(int));` – BLUEPIXY Nov 12 '14 at 19:21
  • 1
    array[size]=(int*)calloc(size, sizeof(int)); this line makes no sense. array should be a pointer not an array, also, remember to free the memory you allocated...., also, why do you create a static array in main and then you call generateFibonacci without saving the allocated buffer???? – DGomez Nov 12 '14 at 19:21
  • 2
    If this is C++, `array[size]` is non-standard, plus you should be using container classes and prefer `new` for memory allocation. Either way, don't cross-tag. – crashmstr Nov 12 '14 at 19:21
  • no this is just c, not c++. – Peter Dowling Nov 12 '14 at 19:24
  • `int *array = generateFibonacci(size);` – BLUEPIXY Nov 12 '14 at 19:29
  • @BLUEPIXY should that statement be in my main? before calling for my function? – Peter Dowling Nov 12 '14 at 19:34
  • @PeterDowling yes, If you do not do that, Return value from the function is discarded. – BLUEPIXY Nov 12 '14 at 19:36
  • Here's another bug: `for(i = 2; i < size+1; i++)` should be `for(i = 2; i < size; i++)` – Weather Vane Nov 12 '14 at 19:40
  • `return *array;` --> `return array;` [fix demo](http://ideone.com/wPVZ6X) – BLUEPIXY Nov 12 '14 at 19:44
  • Okay so after the changes the program now compiles but i get an endless loop of "*"'s instead of the asterisks showing Fibonacci numbers – Peter Dowling Nov 12 '14 at 19:54
  • @PeterDowling Perhaps, you have to input the size of Fibonacci number of overflows or Become number really is greater. – BLUEPIXY Nov 12 '14 at 20:00

1 Answers1

1

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() and scanf()

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 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 of int. 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 of int. 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 the size elements of the array you want to return:

return array[i];//where `i` can be any index value, from 0 to size-1
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 2
    @TheParamagneticCroissant, you are plain wrong. The C standard explicitly allows this form of initialization. Even more, `{0}` is the catch all initializer that is supposed to work for all basic types, arrays, `struct`, whatever. – Jens Gustedt Nov 12 '14 at 20:49
  • @TheParamagneticCroissant - I could not find your assertion in the standard, so ***[asked the question](http://stackoverflow.com/q/26896152/645128)*** on SO. It appears it is not invalid to use this form of initialization. – ryyker Nov 12 '14 at 20:54
  • @JensGustedt comment removed; it still does not do what it was supposed to do. – The Paramagnetic Croissant Nov 12 '14 at 22:22