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]'
You can not assign to an array from an expression:
Instead:
An notice that you are using a local array whose lifetime ends with the function, change to
or better yet
don't forget to call
free
at the end:But why don't you pass the original array to the function?:
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.
The compiler is complaining about the line
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 ofgetACOfNumber
is anint *
, which is not the same type asint [100]
.This could work if you declared
receivedNumbers
asIn that case you're assigning a pointer to a pointer, which should work.
But, you have another problem:
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:Try replacing
int theArray[100]
withint *theArray=malloc(100*sizeof int)
.While internally in C
arrays are pointersarrays 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 tofree
it after use.