Thread: Structs help

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    Structs help

    Ok so this may be an odd problem but here is what I need to accomplish.

    So, I have a struct called enemy

    Code:
    struct enemy {
       int health; 
       int damage; 
    }
    and I want the user of my program to be able to determine how many enemies he wants, so if the user enters 5 at run time I need to create 5 enemy's.

    enemy Enemy1;
    enemy Enemy2;
    enemy Enemy3;
    ...
    ...

    how would I do this?
    I will also need to take these enemys and add them to a list, for example if Enemy2 dies I need to add Enemy2 to a dead list. Thank you so much for the help.

  2. #2
    Registered User
    Join Date
    Oct 2015
    Location
    Turkey
    Posts
    10
    It can be like that

    Code:
    struct enemy {   int health; 
       int damage; 
    
    }


    and then

    Code:
    enemy Enemy1,Enemy2,Enemy3;

  3. #3
    Registered User
    Join Date
    Oct 2015
    Location
    Turkey
    Posts
    10
    by the way I forgot that

    Code:
    struct enemy{
                            int health;
                            int damage;}enemy;

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by sulfur93 View Post
    by the way I forgot that

    Code:
    struct enemy{
                            int health;
                            int damage;}enemy;
    What?

    That makes no sense. Your previous post makes no sense either... this is C and not C++.

    Edit: perhaps you meant

    Code:
    typedef struct enemy{
                            int health;
                            int damage;} enemy;
    Last edited by Hodor; 12-06-2015 at 07:38 AM.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I don't know if you have learned about memory allocation or not.
    So I show a simple code without malloc/calloc.
    Code:
    #include <stdio.h>
    #define MAX_ENEMY 10
    
    typedef struct enemy_s {
       int health;
       int damage;
    } enemy;
    
    int main (void) {
    
        int i;
        enemy enemies[MAX_ENEMY];
    
    // init the enemy-array
        for (i = 0 ; i < MAX_ENEMY ; i++) {
            enemies[i].health = 100;
            enemies[i].damage = 0;
        }
    
        return 0;
    }
    In this example, all enemies are in an array.
    The definition on line 2 fix the maximum.
    If you only play with 5 enemies, assign -1 to enemy[5].health and above.
    Other have classes, we are class

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    typedef struct enemy {
    
       int health;
    
       int damage;
    
    } enemy;
    is perfectly fine. The struct's tag does not have to be different to the typedef (struct enemy and enemy are distinct). Maybe some people think it's more readable, but my opinion differs.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by Hodor View Post
    Code:
    typedef struct enemy {
    
       int health;
    
       int damage;
    
    } enemy;
    is perfectly fine. The struct's tag does not have to be different to the typedef (struct enemy and enemy are distinct). Maybe some people think it's more readable, but my opinion differs.
    I know, but do not confusing myself, i give all structs the sufix '_s'. It is like many types have the sufix '_t'.
    We can also define the struct anonymous.
    Code:
    typedef struct {
       int health;
       int damage;
    } enemy;
    Edit:
    It can be that a newcomer asume that this names must differ, if he/she read only my post.
    Thanks to point that out, Hodor.
    Last edited by WoodSTokk; 12-06-2015 at 02:33 PM.
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread