c - Why are the values of a struct corrupted after the struct is initialized? -


i trying initialize array of structs values, cannot values stay constant. use initial loop retrieve values string , assign them struct in array. once try iterate through array loop, of values not same.

this code in question:

    void printorder(order *node)     {          printf("title is: %s\n",node->title);          printf("price is: $%f\n",node->price);          printf("id is: %d\n",node->custid);          printf("category is: %s\n",node->category);     }      void initorder(order *neworder, char *title, double price, int custid, char   *category)     {          neworder->title = title;          neworder->price = price;          neworder->custid = custid;          neworder->category = category;          neworder->next = null;           printf("new order object initialized\n");     }      char *title;     char *pricetemp;     char *idtemp;     char *category;      order localorders[numorders]; // numorders value found earlier      int k;     for(k = 0; k < numorders; k++)     {              fgets(line,orderlinesize,orders); // orderlinesize found earlier               title = strtok(line,"|");             pricetemp = strtok(null,"|");             idtemp = strtok(null,"|");             category = strtok(null,"|");              price = atof(pricetemp);             id = atoi(idtemp);              localorders[k].title = title;             localorders[k].price = price;             localorders[k].custid = id;             localorders[k].category = category;              order *temp = &localorders[k];              initorder(temp,title,price,id,category);             printorder(temp);     }     order *temp;      for(k = 0; k < numorders; k++)     {             temp = &localorders[k];             printorder(temp);             printf("\n");     } 

here header file order:

#ifndef order_h #define order_h #include <stdlib.h> struct order {     char *title;     double price;     int custid;     char *category;      struct order *next; };  typedef struct order order;  void initorder(order *neworder, char *title, double price, int custid, char *category); void printorder(order *node);  #endif 

for example, first loop print:

title is: "tasting beer: insider's guide world's greatest drink" price is: $11.310000 id is: 5 category is: housing01 

where second loop give me:

title is: "tasting beer: insider's guide world's greatest drink" price is: $19.800000 id is: 2 category is: s guide world's greatest drink" 

why values initializing struct in first loop getting overwritten in second loop? title doesn't seem affected, rest of parameters keep getting overwritten, , i'm not sure why.

strtok() returns pointers character buffer line, overwritten later when fgets() called next time.

to save "components" of tokenized string, can example duplicate strings strdup():

title = strdup(strtok(line,"|")); // ... 

Comments

Popular posts from this blog

c++ - How to add Crypto++ library to Qt project -

jQuery Mobile app not scrolling in Firefox -

How to use vim as editor in Matlab GUI -