Pointer struct syntax confusion

Discussion in '3DS - Homebrew Development and Emulators' started by cal64, Jan 27, 2017.

  1. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    In portal3ds, there are accesses to values in structs such as this one: mdl->frames[n1].verts . However, in that case both frames and verts are struct pointers. But here, the syntax used to access them is ".". How come it's not "->"? I've actually serached online for a bit and couldn't find anything. Also, since that's basically the same thing, what are the syntax rules when using struct pointers? (I.e accessing stored adress vs. Accessing value it's pointing to)

    Thanks for reading, any help is very appreciated!

    Is this even the right place to post this, btw?
     
    Last edited by cal64, Jan 27, 2017
  2. MichiS97

    MichiS97 "Leftist snowflake milennial"

    Member
    GBAtemp Patron
    MichiS97 is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    1,359
    759
    Jun 14, 2011
    Germany
    Munich
    I'm not really sure if I perfectly understand what you mean but the pointer->function/var syntax is C++ specifif while the *pointer.function/var syntax can be used in both C and C++. Both compilers are usable for ctrulib, so portal 3ds is probably compiled in C++
     
  3. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    It's in a .c file
     
  4. Wolfvak

    Wolfvak *yawn*

    Member
    807
    1,062
    Oct 25, 2015
    Uruguay
    This is a generic C question - mdl and frames are pointers, frames[n1] isn't. It's just an md2_frame_t, so you access its elements with "."
     
  5. MichiS97

    MichiS97 "Leftist snowflake milennial"

    Member
    GBAtemp Patron
    MichiS97 is a Patron of GBAtemp and is helping us stay independent!

    Our Patreon
    1,359
    759
    Jun 14, 2011
    Germany
    Munich
    A c file can still be compiled with a C++ compiler
     
  6. evandixon

    evandixon PMD Researcher

    Member
    1,686
    808
    May 29, 2009
    United States
    You use -> if you have a pointer and . otherwise. The array index [n1] gives you the actual structure, assuming the array isn't an array of pointers.
     
    mironicurse and Wolfvak like this.
  7. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    But how come it's accessed from mdl...? Man I'm so sorry that's a lot of questions
     
  8. Wolfvak

    Wolfvak *yawn*

    Member
    807
    1,062
    Oct 25, 2015
    Uruguay
    You're not giving us any context. I don't know what mdl, frames or verts is, so I can't provide a correct answer.
     
  9. evandixon

    evandixon PMD Researcher

    Member
    1,686
    808
    May 29, 2009
    United States
    mdl is a pointer to a structure. The structure contains an array, which is similar to a pointer in some regards. The [] dereferences the pointer and gets you the correct item in the array. Now you have a structure (not a pointer) you can access using the . operator.
     
  10. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    )
    Yes, sorry.
    Mdl is a struct for a 3d model file format (md2). Frames is a part of that struct (the md2 file format contains the model's animations as well, frame per frame, each frame being essentially a different vesion of the model), and verts is the vertex positions for that frame.

    — Posts automatically merged - Please don't double post! —

    But then, how does one tell an array apart from a pointer in the struct definition? The definition for mdl looks like this:
    typedef struct
    {
    md2_header_t header;
    md2_skin_t *skins;
    md2_texCoord_t *texcoords;
    md2_triangle_t *triangles;
    md2_frame_t *frames;
    u8 num_animations;
    md2_anim_t* animations;
    md2_vertperm_t* permutation;
    u32 permutation_size;
    u32 skin_width, skin_height;
    u16* indices;
    }md2_model_t; and as you can see frames appears to be declared as a pointer.
     
  11. elhobbs

    elhobbs GBAtemp Advanced Fan

    Member
    807
    296
    Jul 28, 2008
    United States
    In the structure definition? You can't tell! pointers can be freely accessed as arrays - you need to know the data, or use an API that does.
     
  12. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    Welp, thats just me lacking C knowledge. Still, thank you all for helping on this one!
    So I'm assuming that's the reason it uses "." Instead of "->", right? It's just declared as a pointer and "." / "->" are just the ways it's being accessed, right...? I'm sorry, I just really want to make sure :)
     
    Last edited by cal64, Jan 27, 2017
  13. Wolfvak

    Wolfvak *yawn*

    Member
    807
    1,062
    Oct 25, 2015
    Uruguay
    If you have a pointer to a structure, you use "->" to access its elements. If you have the structure itself (say, you allocated it in stack) then you access its elements with "."

    Example:

    Code:
    typedef struct
    {
        float a;
        int b;
    } struct_t;
    
    ....
    
    struct_t *str_heap = malloc(sizeof(struct_t)); /* Allocated in HEAP, note that str_heap isn't a struct, but a pointer to one (if str_heap were read as a uintptr_t it'd return a location in memory) */
    str_heap->a = 1.5f;
    str_heap->b = (int)str_heap->a;
    
    ...
    
    struct_t str_stack; // Allocated in STACK, str_stack is in fact the struct itself
    str_stack.a = 5.0f;
    str_stack.b = 20;
    
    ...
    
     
    Last edited by Wolfvak, Jan 27, 2017
  14. cal64
    OP

    cal64 Member

    Newcomer
    44
    4
    Oct 31, 2016
    Belgium
    Well I get that, but what about arrays? If it's declared as a pointer, why is it then not accessed using "->"?? Was I right in my previous post?

    Geez I'm so sorry, it feels lile it's so obvious and I'm the only one not getting it...
     
  15. Wolfvak

    Wolfvak *yawn*

    Member
    807
    1,062
    Oct 25, 2015
    Uruguay
    As I previously said, "mdl" and "frames" are pointers, therefore you access their elements with "->". However you're already deferencing "frames" when you do "[n1]" (get the n1-th "frame"). "frames[n1]" isn't a pointer to the structure, it's the structure itself. Same with "frames[X]" (X being any integer within range)