下面的代码有效(没有错误),但是我从下面的代码中得到了奇怪的输出:
输出:
名称为8îk,数量为0
名称为░îk,数量为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);
}
在first
和seconds
已经指针,所以你并不需要通过他们的地址,但它们的价值(该结构他们指着的地址):
display(first);
display(second);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句