• AVR Freaks

Hot!Difference between return pointer type and return array

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:32:58 (permalink)
0

Difference between return pointer type and return array

I am a little confused on the following and have the following code below. 
1) I am trying to get a memory address/a reference/a pointer to a location and confused that if both give me a memory location which one should I declare and use. 
typedef UINT8 EightElements_t[3]

EightElements_t foo1 = {'1', '2','3'};

EightElements_t *GET_addr(void)
{
     return foo1; //Does this return a memory location of the first element of foo??
}

//SECOND SITUATION
UINT8 *foo2[3];
foo2 = {'4', '5', '6'};
UINT8 *GET_addr(void)
{
     return foo2; //Does this also return adress
}

[/code]
#1

4 Replies Related Threads

    Jan Audio
    Super Member
    • Total Posts : 193
    • Reward points : 0
    • Joined: 2018/09/24 08:12:24
    • Location: 0
    • Status: offline
    Re: Difference between return pointer type and return array 2021/03/07 10:32:30 (permalink)
    -2 (2)
    Yes because the characters have to be stored somewhere.
    #2
    rpg7
    Super Member
    • Total Posts : 1445
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Difference between return pointer type and return array 2021/03/08 04:08:04 (permalink)
    0
    An interesting apparent anomaly. Functions can return structs which may consist of an array. 
    Likewise a structure can be assigned to another structure of the same type using the normal assignment symbol (=).

    typedef struct 
    {
          char c;
          int array[10];
    } myStruct;
     
    myStruct a;
    myStruct b;
     
    void main(void)
    {
         a.c = 'A'
         b = a;      // copy the struct
     
       while (1);
    }

    #3
    andersm
    Super Member
    • Total Posts : 2901
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: Difference between return pointer type and return array 2021/03/08 04:42:16 (permalink)
    0
    rpg7An interesting apparent anomaly. Functions can return structs which may consist of an array. 
    Likewise a structure can be assigned to another structure of the same type using the normal assignment symbol (=).

    The compiler knows the size of a struct, so it's easy to generate the correct memcpy code. Arrays, not so much. If you really want to return arrays by value, you can always do it manually.
    #4
    moser
    Super Member
    • Total Posts : 637
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: Difference between return pointer type and return array 2021/03/08 07:10:05 (permalink)
    0
    newb77777
    //SECOND SITUATION
     
    UINT8 *foo2[3];
    foo2 = {'4', '5', '6'};
    UINT8 *GET_addr(void)
    {
         return foo2; //Does this also return adress
    }

    This doesn't make sense to me. I even think it doesn't compile at all.
     
    foo2 is defined as an array with 3 elements of type pointer to UINT8. So you have three pointers in your array. Then you try to assign an array of characters to it. This does not work, unless you do it as initializer, together with the definition. However, even if you did it like this, and if this would somehow go through the compiler, then each of the pointers would now be pointing to an address which is equal the character value of '4', '5' and '6', which is 0x34, 0x35, and 0x36. 
     
    Also Note: 
    // This is a pointer to an array with 3 elements of type UINT8. 
    UINT8 (*pointer)[3];
     
    // This is an array with 3 elements of type pointer to UINT8. 
    UINT8 * pointer[3];

     
     
    Basically, if you write down the name of an array in an expression you will get the pointer to the first element. So:
    foo == &foo[0].
    But in fact, it is not that simple. If you want to really understand, then read the document "Expert C Programming: Deep C Secrets" which is linked in this post:
    https://www.microchip.com/forums/FindPost/1148518
    In particular: Chapter 4. The Shocking Truth: C Arrays and Pointers Are NOT the Same!
     
    post edited by moser - 2021/03/08 07:11:55
    #5
    Jump to:
    © 2021 APG vNext Commercial Version 4.5