Structure Questions

Hi, I'm currently learning about structures and I would like to know what the difference is between initialising a structure like this:
1
2
3
4
5
6
7
8
9
    struct player hero, enemy;

    hero.health = 100;
    hero.attack = 10;
    hero.defense = 10;

    enemy.health = 100;
    enemy.attack = 10;
    enemy.defense = 10;


...and this:
1
2
3
4
5
6
7
8
9
    struct player hero =
    {
        100, 10, 10
    };

    struct player enemy =
    {
        100, 10, 10
    };

Is one better than the other for any particular purpose, or is it just an aesthetically preferential thing?

I would also like to know, is this correct implementation of typedef using structs?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct player
{
    int health;
    int attack;
    int defense;
};

typedef struct player Player;

int main()
{
    Player hero =
    {
        100, 10, 10
    };

    Player enemy =
    {
        100, 10, 10
    };
}


It compiles fine, so I think I've done it right, but I'd like to be sure.

Also, is the sole purpose of using typedef with structures, or variables in general, simply to reduce 'clutter' on the screen?

As I said, I'm new to structures, so I'd just like a few things clarified. Thank you very much!
I would also like to know, is this correct implementation of typedef using structs?
It is in C. It isn't in C++ as it is not needed.

Is one better than the other for any particular purpose, or is it just an aesthetically preferential thing?
For primitive types it just reduces chance for error and is decreases clutter. For complex types it avoids assigning right away after initialization increasing speed.

struct player hero, enemy; This is C, not C++. In C++ struct is not needed
Sorry, I should have stated this is C code.


For primitive types it just reduces chance for error and is decreases clutter. For complex types it avoids assigning right away after initialization increasing speed.

What are primitive and complex types?


In C++ struct is not needed

Are classes used instead of structs in C++?

Just for utmost clarification, is typedef used only to reduce clutter?

Thanks for the help!
What are primitive and complex types?
It is C++ feature. You should have stated that it is C sooner. Basicly it is class which does large chunk of keeping stuff like handling owned memory itself.

Are classes used instead of structs in C++?
I meant keyword struct is not needed here:
1
2
3
4
5
//C++
struct foo
{ int i };

foo bar; //struct is not needed before foo 
In C++ struct are classes. They just have different default access specifier.

is typedef used only to reduce clutter?
In C it is also used to get rid of struct before variable declarations and to make changing underlying type easier:
1
2
typedef int WORD; //32 bit application
typedef long long WORD; //64 bit application 
So you can use just WORD and do not have to replace everything relying on underlying type instead of just one typedef.

Another quirk:
1
2
3
4
5
6
#define d_intp int*
typedef int* t_intp;

int* a, b, c; //a is pointer to int, b and c are int: possible mistake
d_intp d, e, f; //As define is simple text substitution, same problem here: d is pointer, e and f are not
t_intp g, h, i; //all variables are pointers, no chance for error. 
Last edited on
Okay then, so in C is one way of initialising structures better than the other and is typedef used only to reduce clutter?

Also, I'm having some trouble passing struct variables as parameters to functions. When passing the parameters, I don't get any compiler errors, but any modifications I try to make are not happening. Here are what I think are the relevant bits:

1
2
3
4
5
6
7
8
struct player
{
    int health;
    int attack;
    int defense;
};

typedef struct player Player;


1
2
3
// Function Prototypes
int attack(Player, Player);
int defend(Player, Player);


1
2
3
// Function Calls
status = attack(hero, enemy);
status = defend(hero, enemy);


1
2
3
4
5
6
7
8
9
10
// Functions
int attack(Player hero, Player enemy)
{
    ...
}

int defend(Player hero, Player enemy)
{
    ...
}


Inside the function attack() one of the modifications is enemy.health = enemy.health - hero.attack, but when executed nothing changes and I don't know why. Any ideas?
is one way of initialising structures better than the other
No it is not.
is typedef used only to reduce clutter?
No it also makes types easier to change by providing single customisation point.

any modifications I try to make are not happening
You are passing by value: a copy of structure. So any changes to copy will not affect original. In C you are usually expected to pass a pointer to modifiable data.

is one way of initialising structures better than the other
No it is not.
is typedef used only to reduce clutter?
No it also makes types easier to change by providing single customisation point.

Sorry, I posted before I saw your edit.


You are passing by value: a copy of structure. So any changes to copy will not affect original. In C you are usually expected to pass a pointer to modifiable data.

Is it not possible to achieve what I want using only pass-by-value? If it's not then that's fine, I'll try to learn pass-by-reference with structs.
Pass pointer to it by value :). In C you can only pass by value and this creates other object different and unrelated to original.
In C you can normally pass both by value and by reference. I think that is still the case for passing struct variables.

Also, unless I'm misunderstanding what you're saying, there is no way to pass a pointer by value, as passing pointers is in essence passing by reference.
In C you can normally pass both by value and by reference
There is no passing by reference in C.

there is no way to pass a pointer by value, as passing pointers is in essence passing by reference.
No, you can only pass things by value. And value in this case is some memory address which you then access.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void foo(int* x)
{
    *x = 5; //Set value on arbitrary memore address contained in x to 5
    x = NULL; //Set x to NULL. Will not affect original ponter which was passed.
}

int main(void)
{
    int a = 1;
    int* y = &a;
    foo(y);
    //Now value of a is 5 (and value pointed by y)
    //Value of y itself did not change (it is not NULL). 
    //As it was copied to foo and changes to copy are not reflected on original
    return 0;
}
 
x = NULL; //Set x to NULL. Will not affect original ponter which was passed. 

Why set x to NULL? Isn't the memory released automatically once the function ends?

Is this not passing by reference?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>
#include <stdlib.h>

void pass_by_reference(int*);

int main()
{
    int num = 10;
    int *ptr = &num;

    printf("Pointer contents before passing: %d\n", *ptr);

    pass_by_reference(ptr);

    printf("Pointer contents after passing: %d\n", *ptr);

    return 0;
}

void pass_by_reference(int *n)
{
    *n = *n + 10;
}


Also, what's the difference between creating a pointer like this
 
int * ptr;

...this
 
int *ptr;

...and this?
 
int* ptr;

I've seen it used in different ways and as far as I can tell they are interchangeable, but as ever, I just want to be sure.
Also, what's the difference between creating a pointer like this
No difference at all. Just a style preference.

Why set x to NULL?
To illustrate that by changing x itself, you will not change original value which was passed.

Is this not passing by reference?
No, you are passing pointer by value. Pointer is address. Like street and house №. Passing a pointer = scribbling down address from some ad. If you strike out street name and change house number in your piece of paper it will not affect either read place where house is, nor original ad or one of hundreds of copies of that address.

It is important to not call that pass-by-reference because in C++ there is passing by reference and it has different semantics.
Hmm... I think I understand. Does that mean that what I thought of as pass-by-value is actually pass-by-copy and what I thought of as pass-by-reference is actually pass-by-value?

The reason for my confusion is because for an entire year my programming lecturer has been referring to the two different ways of passing parameters as pass-by-value and pass-by-reference.
Last edited on
pass-by-value == pass-by-copy.

C has only one method of passing vriables to functions: by value.

In one case you are passing something by value: making copy of it.
In other case you are passing pointer to it: you copy it too, but you do not care as you interested in its value only: address you can refer to modify another variable.

Calling it pass-by-reference was common in 80-90th, but after adding actual references in C++ in was discouraged to avoid confusion between language features. Now it is usually called "pass-by-pointer" or something similar.
Alright, that clears that up. Thank you very much for all your help, I've learned quite a bit.
Topic archived. No new replies allowed.