c - Different results sending strings directly to macro vs sending it from array -


i think there's basic i'm not understanding(new c) regarding strings. using uthash , works when send strings directly not loop(thats getting data array).

here's example:

enum { max_id_len = 5 }; struct my_struct {     char id[max_id_len];                    /* key */     float price;     ut_hash_handle hh;         /* makes structure hashable */ }; struct my_struct *users = null;  void new_stock(char *user_id, float price) {     //printf("%c - %f \n", *user_id, price);     struct my_struct *s;      s = (struct my_struct*)malloc(sizeof(struct my_struct));     strcpy(s->id, user_id);     s->price = price;     hash_add_str( users, id, s );  /* id: name of key field */ } int main() {     printf("starting.. \n");     new_stock("ibm", 10.2);     new_stock("goog", 2.2);     return 0; } 

that works when try same thing array not(i no errors when compiling).

char *name_all[] =  {"ibm", "goog"}; int name_all_size =sizeof(name_all)/sizeof(char);  float price_all[] =  {10.2, 2.2};  enum { max_id_len = 5 }; struct my_struct {     char id[max_id_len];                    /* key */     float price;     ut_hash_handle hh;         /* makes structure hashable */ };  struct my_struct *users = null;   void insert_data() {     printf("inserting data \n");     int data_loc;     (data_loc=0;data_loc<name_all_size;data_loc++) {         //printf("%s - %f \n", name_all[data_loc], price_all[data_loc]);         //new_stock(name_all[data_loc], price_all[data_loc]);         //new try         struct my_struct *s;         s = (struct my_struct*)malloc(sizeof(struct my_struct));         strcpy(s->id, name_all[data_loc]);         s->price = price_all[data_loc];         //printf("%s - %f \n", s->id, s->price); //ahh displays correctly still fails         hash_add_str( users, id, s );  /* id: name of key field */      } }  int main() {     insert_data();         return 0; } 

i'm new c assessing wrong think has way i'm passing variables. when first tried sending new_stock function displaying first char, around problem of passing variable moved contents of function function using add data still same problem.

any idea of i'm doing wrong?

also out of personal interest there tools warns me of problems in code? find gcc helpful once warnings stop have no idea how troubleshoot. there me catch problems earlier(something more verbose gcc). not sure if possible wanted ask.

here's error:

char *name_all[] =  {"ibm", "goog"}; int name_all_size =sizeof(name_all)/sizeof(char); 

the array name_all array of char pointers, not char, , therefore eight-bytes if you're on 32-bit system 32-bit pointers, , 16-bytes on 64-bit system 64-bit pointers. keep in mind string literals being pointed not stored in array, pointers string literals in array. want is:

int name_all_size = sizeof(name_all)/sizeof(char*);                                             ^^^^^ note pointer type 

that should give value of 2, correct number of elements in name_all.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -