下面的代碼工作(沒有錯誤),但我得到了奇怪的輸出,因爲這從下面的代碼:下面代碼給出了奇怪的輸出
OUTPUT:
名稱是8îk和數量是0
名稱是數量和數量是2130567168
我的錯誤在哪裏?
#include <stdio.h>
#include <stdlib.h>
void display(struct item *);
struct item{
char name[50];
int quantity;
};
int main(void){
struct item *first = (struct item *)malloc(sizeof(struct item));
strcpy(first->name, "Banana");
first->quantity = 32;
struct item *second = (struct item *)malloc(sizeof(struct item));
strcpy(second->name, "Apple");
second->quantity = 432;
display(&first);
display(&second);
getch();
}
void display(struct item *i){
printf("Name is %10s and quantity is %7d\n", i->name, i->quantity);
}
非常感謝答案 – Lyrk