2

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]'

2
  • 2
    It's not a "funny mistake", but a serious one. You are trying to return a locally allocated object. Once the function returns, there is no more theArray exist. Commented Sep 23, 2016 at 17:38
  • 2
    Fyi, theArray doesn't exist anymore once your function returns, so returning its address is pointless, You need a static, a global, dynamic allocation, or a caller-provided buffer. Choose. Commented Sep 23, 2016 at 17:39

4 Answers 4

4

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) {
Sign up to request clarification or add additional context in comments.

3 Comments

The static approach will work only if this function called once, and it is not expected it to return more than one different objects..
Thank you really much for the solution.But i am not 100% sure what is "sizeof".
sizeof gets the size of any object or primitive type
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 Comments

While internally in C arrays are pointers, - this is going to get you into troubles
This is reasonable advice and analysis, except that C arrays are not at all the same thing as pointers. It does not, however, address the compiler diagnostic about which the OP inquired.
Fixed to remove ambiguity
1

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.

1 Comment

Actually,i know that.But you can see the other answers.You can return the pointer.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.