-->

Return Array in C?

2019-02-15 18: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[MAXSIZE];
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  recievedNumbers = getACOfNumber(256);
  for (int i = 0; i < sizeof(recievedNumbers) / 8; i++) {
    Serial.print(recievedNumbers[i]);
  }
  Serial.println();
}

int* getACOfNumber(int theNumber) {
  bool done = false;
  int i = 0;
  int theArray[100];
  while (!done) {
    if (theNumber % 2 == 0) {
      theNumber = theNumber / 2;
      theArray[i] = 2;
    } else if (theNumber % 3 == 0) {
      theNumber = theNumber / 3;
      theArray[i] = 3;
    }
    else if (theNumber % 5 == 0) {
      theNumber = theNumber / 5;
      theArray[i] = 5;
    }
    else if (theNumber % 7 == 0) {
      theNumber = theNumber / 7;
      theArray[i] = 7;
    } else {
      theArray[i] = theNumber;
      done = true;
    }
    i++;
  }
  return theArray;
}

Error Message :

AC:10: error: incompatible types in assignment of 'int*' to 'int [100]'

exit status 1 incompatible types in assignment of 'int*' to 'int [100]'

回答1:

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) {


回答2:

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.



回答3:

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.



回答4:

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
  }
}