c - How to dereference from an array of struct poiners -


say have struct:

struct card{   char num;    char suit;  }; 

and array of struct pointers:

struct card *deck[52]; 

and method intialize deck of cards (this device drive hence kmalloc):

static int __init_deck(void){ // allocate memory     int i;     for(i = 0; < 52; i++){         deck[i] = kmalloc(sizeof(struct card), gfp_kernel);         if(!deck[i]){             printk(kern_err "unable allocate memory.");         }     }      returnvalue = misc_register(&deck_dev);     if(returnvalue){          printk(kern_err "unable register\n");     }  // give card structs values in deck.  // spades     for(i = 0; < 13; i++){                   // why not work??         *(deck[i].num) = (char)i;         *(deck[i].suit) = 's';     } // hearts     for(i = 13; < 16; i++){         deck[i].num = (char)i;         deck[i].suit = 'h';     } // diamonds     for(i = 26; < 39; i++){         deck[i].num = (char)i;         deckk[i].suit = 'd';     } // clubs     for(i = 39; < 52; i++){         deck[i].num = (char)i;         deck[i].suit = 'c';     }      return returnvalue;   } 

i declared array of size 52 contains pointers struct card. trying deference struct contained @ position in array can add "stuff" struct. why don't dereferences have them in 1 case work?

you dereferencing here incorrectly, *(deck[i].num) = (char)i;.

first have dereference pointer access member.

 (*deck[i]).num = (char)i; 

or using -> operator:

 deck[i]->num = (char)i; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -