Return Array in C?

前端 未结 4 685
春和景丽
春和景丽 2021-01-07 05:21

I cant return array in c,i am quite new to C so i probably do some kind of funny mistake, here is the code:

#define MAXSIZE 100
int recievedNumbers[MAXSI         


        
相关标签:
4条回答
  • 2021-01-07 05:32

    In C, you can't return an array from any function, but you don't need to do so too. Because you can pass the array to the funtion (the array's reference will be sent) and change whatever you want in the array. The change(s) will stay in the array even if the program comes out of that function. Thank you.

    0 讨论(0)
  • 2021-01-07 05:36

    Try replacing int theArray[100] with int *theArray=malloc(100*sizeof int).

    While internally in C arrays are pointers arrays and pointers look very similar, they are of different type - this is what compiler is complaining about. Additionally, the compiler has saved you from a painful memory corruption error:

    when you define a local array variable inside your function, it gets memory allocated on function's stack. This gets released when function ends, so your result either becomes invalid or may become invalid later, or worse, cause various segmentation faults. malloc allocates memory in global application heap, it won't go bad after function terminates. But then, don't forget to free it after use.

    0 讨论(0)
  • 2021-01-07 05:42

    The compiler is complaining about the line

    recievedNumbers = getACOfNumber(256);
    

    You cannot use the = operator to assign the contents of an array; an array expression may not be the target of an assignment operation. Also, the result of getACOfNumber is an int *, which is not the same type as int [100].

    This could work if you declared receivedNumbers as

    int *recievedNumbers;
    

    In that case you're assigning a pointer to a pointer, which should work.

    But, you have another problem:

    int* getACOfNumber(int theNumber) {
      bool done = false;
      int i = 0;
      int theArray[100];
      ...
      return theArray;
    }
    

    This will not do what you expect. Once the getACOfNumber function exits, theArray no longer exists - the pointer you return is no longer valid.

    IMO, your best bet is to pass the array as a parameter to getACOfNumber and update it directly in the function:

    getACOfNumber( 256, receivedNumbers, MAXSIZE );
    ...
    void getACOfNumber( int number, int *theArray, size_t max )
    {
      bool done = false;
      size_t i = 0;
      while ( i < max && !done )
      {
        ... // use existing code
      }
    }
    
    0 讨论(0)
  • 2021-01-07 05:52

    You can not assign to an array from an expression:

    int recievedNumbers[MAXSIZE];
    ...
    recievedNumbers = getACOfNumber(256);
    

    Instead:

    memcpy(receivedNumbers, getACOfNumber(256), sizeof(receivedNumbers));
    

    An notice that you are using a local array whose lifetime ends with the function, change to

    static int theArray[100];
    

    or better yet

    int *theArray = calloc(100, sizeof(*theArray)); /* Zero initializes the array */
    

    don't forget to call free at the end:

    int *temp = getACOfNumber(256);
    
    memcpy(receivedNumbers, temp, sizeof(receivedNumbers));
    free(temp);
    

    But why don't you pass the original array to the function?:

    getACOfNumber(receivedNumbers);
    ...
    void getACOfNumber(int *theArray) {
    
    0 讨论(0)
提交回复
热议问题