• AVR Freaks

Helpful ReplyHot!Typecasting in C - getting rid of warnings

Author
Tinkerer7777
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2015/04/04 01:46:02
  • Location: 0
  • Status: offline
2021/03/07 09:50:19 (permalink)
0

Typecasting in C - getting rid of warnings

typedef UINT8 EightElements_t[3]

EightElements_t foo1 = {'1', '2','3'};
UINT8 *foo2[3];
foo2 = {'4', '5', '6'};

EightElements_t *GET_addr(void)
{
     return foo2; //Compiler doesn't like this and gives warning.
}

//SECOND SITUATION



 
Code above gives compiler warnings. I am casting type even though the underlying type in UIN8.
#1
blue_led
New Users
  • Total Posts : 143
  • Reward points : 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 10:05:33 (permalink)
+1 (1)
Arrays can't be returned by functions.
 
Duplicate https://www.microchip.com/forums/FindPost/1168189
post edited by blue_led - 2021/03/07 10:46:00
#2
Murton Pike Systems
Super Member
  • Total Posts : 247
  • Reward points : 0
  • Joined: 2020/09/10 02:13:01
  • Location: 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 11:55:58 (permalink)
-1 (1)
blue_led
Arrays can't be returned by functions.

 
Not yet !
 
You can in Visual Studio C#.

int[] ppp()
{
int[] sdata5 = new int[SYMBOLDATALEN];

return sdata;

}
#3
Tinkerer7777
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2015/04/04 01:46:02
  • Location: 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 14:31:54 (permalink)
0
blue_led
Arrays can't be returned by functions.
 

Okay but why not? Isn't array already passed by memory references and the array container is filled by the type of data that it is to hold?
 
Can structs be returned by functions? Array and structs are very similar except array can only hold one type of data eg char or UINT8/16/32 in C but structs can hold multiple types of data. 
 
#4
blue_led
New Users
  • Total Posts : 143
  • Reward points : 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 16:03:02 (permalink) ☄ Helpfulby Tinkerer7777 2021/03/07 16:35:47
+1 (1)
Here it is Microchip world not Microsoft.
 
13.8  FUNCTION RETURN VALUES
https://ww1.microchip.com...eviceDoc/50001686J.pdf
#5
cirilo.b
Senior Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2020/09/08 18:40:42
  • Location: 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 16:48:35 (permalink)
0
Why can't arrays be returned by functions? Easy - because the C language has a single return value and while a pointer to an array is a single value, arrays are not pointers. Consider this:
 
void funcA(char const *text)
{
  printf("Size of text: %d\n", sizeof(text)); /* text is a pointer, not an array - size = size of pointer */
}
 
int main()
{
  char text[80];  /* text is an array */
  printf("Size of text: %d\n", sizeof(text)); /* size of array is 80 bytes (well, if char = 1 byte) */
  return 0;
}
 
#6
1and0
Access is Denied
  • Total Posts : 12265
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 20:15:42 (permalink)
+1 (1)
newb77777
blue_led
Arrays can't be returned by functions.

Okay but why not? Isn't array already passed by memory references and the array container is filled by the type of data that it is to hold?

Because the C language standard forbids a function to have an array return type:
C Standard
A function declarator shall not specify a return type that is a function type or an array type.

 
 
newb77777
Can structs be returned by functions? Array and structs are very similar except array can only hold one type of data eg char or UINT8/16/32 in C but structs can hold multiple types of data. 

Struct can be returned by function, but NOT array. ;)
 
 
#7
LdB_ECM
Super Member
  • Total Posts : 457
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Typecasting in C - getting rid of warnings 2021/03/07 20:41:59 (permalink)
0
You can walk around the issue on your initial code
EightElements_t *GET_addr(void)
{
     return (EightElements_t *)&foo2[0];
}

 
In C any pointer to a type is also a pointer to an array of that type.
Your responsibility is the pointer is to 3 bytes.
post edited by LdB_ECM - 2021/03/07 20:46:25
#8
Jump to:
© 2021 APG vNext Commercial Version 4.5