Thread: warning message "missing braces around initalizer"

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    warning message "missing braces around initalizer"

    i have the following struct declared and a function to put values into it
    Code:
    typedef struct
    {
        enum Suit suit;
        enum Card_Value card;
        char card_code[10];
    } Card;
    
    void shuffle_deck(Card *p_deck_index)
    {
        int count_cards = 0, card_found = 0;
        int i;
        int suit_value, face_value;
        char *temp_string;
    
        while (count_cards < 52)
        {
            suit_value = rand() % 4;
            face_value = rand() % 13 + 2;
            for (i = 0; i < count_cards + 1; i++)
            {
                if (p_deck_index[i].suit == suit_value && p_deck_index[i].card == face_value)
                {
                    card_found = 1;
                    break;
                }
            }
            if (!card_found) //card_found is 0
            {
    /*
                p_deck_index[count_cards].suit = suit_value;
                p_deck_index[count_cards].card = face_value;
                temp_string = get_card_code(suit_value, face_value);
                strcpy(p_deck_index[count_cards].card_code, temp_string));
                count_cards ++;
    //*/
                temp_string = get_card_code(suit_value, face_value);
                p_deck_index[count_cards++] = (Card) {suit_value, face_value, *temp_string}; //<--- generates warning here
            }
            else
            {
                card_found = 0;
            }
        }
    }
    if i use the commented out part it all compiles fine. if i use the other two lines of code i get the warning. what am i missing please
    coop

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    "temp_string" is a pointer to char, "*temp_string" is a char.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    if i remove the " * " i get the usual warning about assignment to pointer from int without cast (see i get it so often i know it off by heart lol)

    if i was printing out the temp string i would do printf("%s\n", temp_string); ie passing the address of where temp string is stored in memory to printf
    coop
    Last edited by cooper1200; 05-11-2019 at 05:16 AM.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    it helps if i make the member card_code a pointer i guess
    Code:
    int x , *px = &x, *pdiff;
    x=3; //*px points to the address of x which now holds the value 3
    *pdiff = px ;// pdiff now points to the address of px which points to the address of x
    printf(""%d\n", pdiff); //prints 3 on the screen
    coop

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    If you want a deep copy, meaning that you don't copy the pointer but the data it points to, you're going to have to use strcpy. The initialization list isn't smart enough to do that for you.

    For example:
    Code:
    p_deck_index[count_cards] = (Card) {suit_value, face_value, malloc(strlen(temp_string)+1)};
    strcpy(p_deck_index[count_cards++].card_code, temp_string);
    
    // don't forget to free() the malloced memory when you're done using it
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    that didnt work it made all the cards the same code because the contents of the address it was pointing to changed. if i change the member to be a pointer to an array of cards ie
    Code:
    *card_code[53][10] how do i initialize that with (card) {suit_value, card_value, ?}
    coop

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    sorry you answered as i was typing the question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-12-2011, 06:59 PM
  2. Replies: 9
    Last Post: 05-28-2010, 10:11 AM
  3. Replies: 2
    Last Post: 02-28-2008, 11:51 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread