Simple structure + pointer question
Hi,
I do not understand why the output of the following code like that:
Name = Apple Pie
ID = 123
Price = 1.65
Name = Greggs Coffee //do not understand
ID = 733 //why the value will be back to the static
Price = 3.20 //Was it caused by the keyword "static"?
to hold the variable constant??
#include <stdio.h>
#include <string.h>
struct sample {
char *name;
int *id;
float price;
};
static char product[] = "Greggs Coffee";
static float price1 = (float) 3.20;
static int id = 733;
void printrecord(struct sample *goods)
{
printf("Name = %s\n", goods->name);
printf("ID = %d\n", *goods->id);
printf("Price = %.2f\n", goods->price);
goods->name = &product[0]; //Greggs Coffee
goods->id = &id; //733
goods->price = price1; //3.20
}
main()
{
int code = 123, number;
char name[] = "Apple Pie";
struct sample item;
item.id = &code; //123
item.price = (float) 1.65;
item.name = name; //Apple Pie
number = *item.id; //123
printrecord(&item);
printf("Name = %s\n", item.name); //Apple Pie
printf("ID = %d\n", *item.id); //123
printf("Price = %.2f\n", item.price); //1.65
return 0;
}
Thanks for help.
gogo