Reputation: 1579
I am trying to initialize an array of structs with some values, but I cannot get the values to stay constant. I use an initial for loop to retrieve the values from a string and assign them to a struct in an array. Once I try to iterate through that array with another loop, some of the values are not the same.
This is the 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 is a value found earlier
int k;
for(k = 0; k < numOrders; k++)
{
fgets(Line,orderLineSize,orders); // orderLineSize was 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 is the header file for an 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, the first loop would print:
Title is: "Tasting Beer: An Insider's Guide to the World's Greatest Drink"
Price is: $11.310000
ID is: 5
Category is: HOUSING01
where as the second loop would give me:
Title is: "Tasting Beer: An Insider's Guide to the World's Greatest Drink"
Price is: $19.800000
ID is: 2
Category is: s Guide to the World's Greatest Drink"
Why is are the values that I am initializing the struct with in the first loop getting overwritten in the second loop? The title doesn't seem to be affected, but the rest of the parameters keep getting overwritten, and I'm not sure why.
Upvotes: 0
Views: 1599
Reputation: 17573
You need to allocate memory for each of the data elements. What you are doing is using the same memory area for your strings and just remembering pointers to those areas rather than using new memory areas for each string.
So you need to malloc() a new memory area for each of the records read in so that each record has its own unique memory area rather than all records sharing the same memory area.
Upvotes: 0
Reputation: 539725
strtok()
returns pointers into the character buffer Line
, which is overwritten later
when fgets()
is called the next time.
To save the "components" of the tokenized string, you can for example duplicate the strings with strdup()
:
title = strdup(strtok(Line,"|"));
// ...
Upvotes: 1