Thread: referening typedef enum then assigning it to another var to reference it?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    referening typedef enum then assigning it to another var to reference it?

    I hope I got all of that terminology correct and in the correct order.

    I've only found how to set up an typedef enum but nothing even close to how to manipulate it in the fashion I want to.

    What I got setup to where I am now only getting this warning, but I believe what I have is still not workable. ( because seg faults say a lot)

    warning: initialization makes pointer from integer without a cast [-Wint-conversion]|
    The basic parts are this:
    Code:
    typedef enum Tile_Image
    {
        TILE, TILEH, TILEV, TILEHV, RTILE, RTILEH, RTILEV, RTILEHV, SPACE_TILE
    }Tile_Image;
    
    // the elements would be assigned 0 thru 8 by default.
    //assigning one of the elements numbers to a char to reference it later.
    
    typedef struct { 
    unsigned char mode;
     }Some_Items;
    
    Tile_Image ti;
    Some_items si;
    
    int *i = (rand() % 8);
    si.mode = ti[i];
    Basically I am trying to get it to randomize 0 ... 8 then whatever element is a match for that random number to be then assigned to the mode in order to reference from it when passed to the function that reads what value si.mode is.

    Code:
    switch (si.mode)
    {
    case TILE:
        do_something;
        break;
    case TILEH:
       do_something;
       break;
    .....
    default:
      break;
    }
    I am just trying to assign the element number to mode so I can reference it again on the other end to set it in motion.

    my logic is because when calling to see what element mode has is.

    printf("%d\n", (int)mode); which prints a number or integer and not the actual name of the element.

    I hope that makes sense to someone in here.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why is i a pointer? It should just be an integer.

    Why are you treating an enum as an array? An enum is not an array. An enum just provides a bunch of named constants. That's all.

    All you need to do to achieve your goal is:
    Code:
    typedef enum {
        TILE,  TILEH,  TILEV,  TILEHV,
        RTILE, RTILEH, RTILEV, RTILEHV,
        SPACE_TILE,
        NUM_TILES  // this symbol will equal the number of previous symbols
    } Tile_Image;
    
    typedef struct { 
      Tile_Image mode;
    } Some_Items;
    
    Some_items si;
    
    si.mode = rand() % NUM_TILES;
    Last edited by algorism; 09-01-2017 at 03:35 PM.
    Explode the sunlight here, gentlemen, and you explode the entire universe. - Plan 9 from Outer Space

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    all good questions, and I did already do what you wrote,
    or, I already did what you have written.
    Code:
        img.which_image_type = (rand() % 4);
        
        switch (img.which_image_type)
        {
            case 0:
                break;
            case 1:
                printf("standard\n");
                img.mode = (rand() % 3);
                set_image_type();
                break;
            case 2:
                printf("tile\n");
                img.mode = (rand() % 8);
                set_image_type();
                break;
            case 3:
                printf("flip\n");
                img.mode = (rand() % 4);
                set_image_type();
                break;
            case 4:
                break;
            default:
                break;
    
    
        }
    then wondered them very same questions you posted myself before I read this post, but figured I'd leave the Q up just in case their was a different way of doing this.

    the pointer thing was because before posting it was giving me this pointer error so I made the int a pointer that got rid of that message and gave me a new one.
    Last edited by userxbw; 09-01-2017 at 05:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe with enum. Assigning a char to an enum value.
    By Iceboxes in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2013, 09:58 AM
  2. how do you print a typedef enum?
    By -EquinoX- in forum C Programming
    Replies: 1
    Last Post: 10-19-2008, 08:50 PM
  3. typedef'd enum specifying type
    By trillianjedi in forum C Programming
    Replies: 4
    Last Post: 05-27-2008, 01:31 PM
  4. need help with typedef enum
    By Lince in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 01:14 PM
  5. typedef enum question
    By .shifty in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2003, 04:43 AM

Tags for this Thread