Passing array return value

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi there,

uint8_t anyfunction(void)[]
{
	uint8_t anything[4]={1,2,3,4};
	return anything;
}

int main(void)
{
	uint8_t somevar[4];
	somevar=anyfunction();
	while(1);
	return 0;
}

Of course this code generates compilation error code "incompatible types in assignment" :D
Above code just an example. I want to pass return value from "anyfunction" which is an array to the "somevar" variable (array too). how to do this? i've searched down the forum but didn't get any usefull clue. Is this problem has any relationship with pointers and typecasting? Any idea?

Thanks.

KISS - Keep It Simple Stupid!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First, it should be:

uint8_t *anyfunction()
{
   uint8_t anything[4]={1,2,3,4}; 
   return anything; 
}

But it still won't do you any good with the code above since "anything" is a local variable. It will disappear as soon as the function returns. You could create the array dynamically with malloc(), but then you need to make sure that you delete it at some time with free().

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But then, how do i assign the function with "somevar"?

uint8_t *anyfunction()     // this one already fixed
{
   uint8_t anything[4]={1,2,3,4};
   return anything;
}

int main(void)
{
   uint8_t somevar[4];
   somevar=anyfunction();  //the problem still persist here
   while(1);
   return 0;
} 

KISS - Keep It Simple Stupid!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Pass in a pointer to the calling function's allocated storage as a parameter:

void anyfunction(uint8_t* somevar)     // this one already fixed
{
   uint8_t anything[4]={1,2,3,4}; 

   memcpy(anything, somevar, 4);
   return;
}

int main(void)
{
   uint8_t somevar[4];
   anyfunction(somevar);
   while(1);
   return 0;
}

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This should work.

Regards
Sebastian

uint8_t *anyfunction()     // this one already fixed
{
   static uint8_t anything[4]={1,2,3,4};
   return anything;
}

int main(void)
{
   uint8_t *somevar;
   somevar=anyfunction();
   while(1);
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are still seriously wrong.

uint8_t *anyfunction()     // this one already fixed
{
   static uint8_t anything[4]={1,2,3,4}; // STATIC
   uint8_t *any;                 // normal pointer
   any = (uint8_t *)malloc(4);   // read docs
   if (any == NULL) fatal_error();
   any[0] = 1; any[1] = 2; any[2] = 3; any[3] = 4;
   return any;      // new dynamic memory
   return anything; // always the same static memory
}

int main(void)
{
   uint8_t *somevar;       // pointer NOT array
   somevar=anyfunction();  // this should work
   while(1);               // forever
} 

Look up dynamic memory in your C textbook. Look up asctime() in a standard library documentation for a function that returns a string from a static buffer.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Dean,
after struggling a little bit, Yes it works!
But um, there is a slight error on the memcpy part.
In order to work, it should be:

   memcpy(somevar, anything, 4);

memcpy(dest, src, len) -> copies from src to dest as much as len.
Thanks, you're great! :D

Umm(again), i'm curious, is there any methode for passing array from function beside using parameter?

KISS - Keep It Simple Stupid!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Wow, all of you are great! It works! Thanks everybody :D

KISS - Keep It Simple Stupid!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

memcpy(dest, src, len) -> copies from src to dest as much as len.
Thanks, you're great! Very Happy

I was writing directly into the forum reply window - should have looked up the API first.

Quote:

Umm(again), i'm curious, is there any methode for passing array from function beside using parameter?

The only other method is the malloc/free system of dynamic memory management, but it has cavets -- it's slower to allocate, requires a lot of flash and can cause problems if you forget to free the memory afterwards.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!