结构打印垃圾中的字符数组

时间:2013-04-12 05:48:12

标签: c string struct initialization

当我运行它时,partName char数组打印垃圾字符。任何想法为什么?谢谢!

typedef struct {
char partName[30]; 
int partNumber; 
float price; 
int stock; 
int reorder;
} inventory;
struct address{
char streetAddress[25]; 
char city[20];
char state[3]; 
char zipCode[6];
};
void GetData(){

inventory order;
address orderAddress;
printf("Enter the part number (Database Includes orders 0001 - 0005):  ");

scanf("%d", &order.partNumber);

if(order.partNumber == 0001){
order.partName == "Pokeball";
order.price == 200;
order.stock == 8263;
order.reorder == 888273;

orderAddress.streetAddress == "21 Oak St.";
orderAddress.city == "Kanto Town";
orderAddress.state == "IA";
orderAddress.zipCode == "28832";

printf("Part was: %s ", order.partName);
//printf("Order was sent to: \n %s\n %s\n %s\n %s\n", orderAddress.streetAddress,         orderAddress.city, orderAddress.state, orderAddress.zipCode);
 }

它正在打印一些随机字符,我确定这是一个简单的错误。

谢谢大家!

1 个答案:

答案 0 :(得分:2)

以下内容不是分配。它们是比较

order.partName == "Pokeball";
order.price == 200;
order.stock == 8263;
order.reorder == 888273;

orderAddress.streetAddress == "21 Oak St.";
orderAddress.city == "Kanto Town";
orderAddress.state == "IA";
orderAddress.zipCode == "28832";

要分配整数,请写

order.price = 200;

要分配给C字符串,请使用strcpy()