Share This Page

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)

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++

But how come it's accessed from mdl...? Man I'm so sorry that's a lot of questions

Click to expand...

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.

Click to expand...

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.

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.

Click to expand...

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.

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

Click to expand...

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.

Click to expand...

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

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

Click to expand...

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

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

Click to expand...

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)