Homebrew Pointer struct syntax confusion

  • Thread starter Thread starter cal64
  • Start date Start date
  • Views Views 1,523
  • Replies Replies 14

cal64

Well-Known Member
Newcomer
Joined
Oct 31, 2016
Messages
65
Reaction score
8
Trophies
0
Age
33
XP
466
Country
France
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,
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++
 
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++
It's in a .c file
 
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 "."
 
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 "."
But how come it's accessed from mdl...? Man I'm so sorry that's a lot of questions
 
But how come it's accessed from mdl...? Man I'm so sorry that's a lot of questions
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.
 
)
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.
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.
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.

--------------------- MERGED ---------------------------

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.
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.
 
)


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.

--------------------- MERGED ---------------------------


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.
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.
 
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.
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,
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 :)
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,
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...
 
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...
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)
 

Site & Scene News

Popular threads in this forum