Post tutorial Report RSS Structures - Arrow or Dot?

This tutorial will teach you a bit about Structures and their uses. I will be using Quake2 as an example, but any game will have some similiarities with this code.

Posted by on - Intermediate Server Side Coding

[page=Introduction]
Hello again!

This tutorial will teach you a bit about structures and how they are used. I barely know anything about how C++ works, or how C works actually, I am just a modder, I don't work with Windows apps (not yet atleast, I am in the progress of one..).

Alright, the main purpose was for structures. Every structure needs to be linked together with another structure to make a pointer (either seen as an arrow -> or a dot .), such as (Quake2) ent->client, or ent->client->resp.

Now, you can use either, but you can use them in different ways to make them better.

[page=Simple Structures]
Here's an example of a simple structure.

struct teststruct
{
	int test1;
	float test2;
	BOOL test3;
};

As you can see, this structure is pretty much nothing. It can be used for like a character settings, like so:

struct character
{
	int age;
	int height;
	int weight;
	char *name;
	char *ally;
};

Let's go to the next page, to learn about Arrow pointers!

[page=Arrow Pointers]
Arrow Pointers. These are usually used the most in engines or very important structures.
Main structures usually are linked with arrows.

Here is an example of the character one, but with an arrow pointer:

First, we need to add this new structure above the character one:

struct character_build
{
	int height;
	int weight;
};

Now, let's edit our character structure to fit this.

struct character
{
	int age;
	struct character_build *char_build;
	char *name;
	char *ally;
};

Now, let's make a function to go with this.

void SetCharAge (character *charac)
{
	charac->age = 21;
	charac->char_build->height = 8.3;
	charac->char_build->weight = 180;
}

That will set those to the specified numbers.
Next page, let's learn when and when not to use dot pointers!

[page=Dot Pointers]
Dot pointers are like arrow pointers, but are usually used for stuff less important.

Now, let's say we want a new structure thing for effects. (It's not real code though..) Let's try this. In your opinion, which will use a dot; effects, or the char_build?

typedef struct
{
	int r;
	int b;
	int g;
} color_s;

There is a struct with three colors. Of course they are just ints, but oh well.

struct effect_s
{
	color_s flash;
	color_s muzzle_color;
};

There is the effects struct. It makes dot references to color_s.

typedef struct
{
	int height;
	int weight;
	int bounding_box[3];
} character_build;

I added one new part to this. It's an array for a bbox. I recommend using vectors with a 3D engine, not an int ;)

struct character
{
	int age;
	character_build char_build;
	char *name;
	char *ally;
	struct effect_s *effects;
};

Here's the heart of the code.
Now, what happens here is this.

It first knows char_build, and knows it's a dot pointer now, so you will use it as so:

character->char_build.height = 8.3;

for example.

Now, the effects works a bit different.

character->effects->flash.r = 8.44;

Personally, I use arrow pointers to lead to a structure with more structures, while using dot pointers for structures with only variables and ints.

[page=How to make them?]

Now, you're wondering, How do I make arrow/dot pointers, and what's the difference in the codes? Well, here's how you do it.

Arrow pointers:

struct structure_name
{
	<<code stuff here>>
}

Dot pointers:

typedef struct
{
	<<code stuff here>>
} structure_name;

Linking them;

Arrow pointers:

struct structure_name *linked_name;

Dot pointers:

structure_name linked_name;

the structure_name in linking is the name of the struct.
The linked_name can be different. It is used in the final structure, such as:

char-> |-----------------|
| linked_name |
|-----------------|

[page=Conclusion]

Hope you learned something. That's my aim!
Later everyone!

-Paril Kalashnikov

Post comment Comments
denreaper
denreaper - - 52 comments

How is "structure_name linked_name;" not an object, but instead a memory pointer?

Reply Good karma Bad karma+1 vote
alexgordon
alexgordon - - 1 comments

Half of the infomation here is incorrect.

First of all -> and . are operators not pointers. Since your terms "Arrow Pointers" and "Dot Pointers" are very confusing I will refer to them as the arrow operator and the dot operator.

The main difference between the arrow and the dot is the dot used on the structure itself and the arrow is used on pointers to the structure. So if you had a struct called "character" like in your examples for instance:
struct character
{
int age;
int height;
const char *name;
}

Then some code to create a new "character" might be:
void foo()
{
//Creates a new character
struct character myCharacter;

//Sets myCharacter's age
myCharacter.age = 5;

//Sets myCharacter's name
myCharacter.name = "Bob";
}

The important things to note are:
We create a new character with "struct character <<variableName>>"
We are not creating a pointer to a struct but an actual struct
And because we don't have a pointer we use the dot operator to access members

The arrow operator is used for getting or setting a member of a struct when you only have a pointer. The important thing to note is that:
aStruct->aMember
Is EQUIVALENT TO
(*aStruct).aMember

So if we have an example (using the struct from before):
void foo(struct character *aPointerToACharacter)
{
//Sets myCharacter's age
aPointerToACharacter->age = 5;

//Sets myCharacter's name
myCharacter->name = "Bob"; //You would probably want to free() or delete whatever was in there before...
//but that is out of the scope of this comment
}

Hopefully this comment has been helpful to someone somewhere :)

Reply Good karma Bad karma+1 vote
QwazyWabbit
QwazyWabbit - - 3 comments

In fact, an expression involving the member-selection operator (->) is a shorthand version of an expression using the period (.) if the expression before the period consists of the indirection operator (*) applied to a pointer value.

expression -> identifier
is equivalent to
(*expression) . identifier
when expression is a pointer value.

In other words, when using a pointer to a structure, you use the member operator (->) and when accessing the structure itself (by reference) you use the dot operator.

So, if we have a struct:

struct structure {
int member1;
int member2;
};

and we access it this way:

ptr_to_struct = &structure;
ptr_to_struct->member1 = 1;

otherwise we would access the member of the instance of the structure:
structure.member1 = 1;

Typically in game mods, especially Quake 2, you will find structures defined as a typedef. In this case it's used to to create pointers to complex variables of that typedef:

typedef struct var_s // compound variable type (storage is not allocated for it at this time)
{
int intval;
float fval;
} var_t;

void somefunc()
{
var_t *p_str; //p_str is a pointer to a structure of type cvar_t (storage is allocated on the stack for it)

p_str->intval = 1;
p_str->fval = 3.14149;
}

If we define the pointer outside of any function, it becomes globally accessible and storage is allocated when we declare it. In this case the compiler will create space for it in the program's data segment.

var_t *p_str; //p_str is a pointer to a structure of type var_t (storage is allocated in the program data memory)

void somefunc()
{
p_str->intval = 1;
p_str->fval = 3.14149;
}

Where Quake2 uses the dot operator, it's usually for global game structures where there is only one instance of that structure.

game.clients
globals.edict_size or globals.num_edicts

Reply Good karma Bad karma+1 vote
Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: