【发布时间】:2013-12-20 15:30:04
【问题描述】:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct BOOK{
char name[15];
char author[33];
int year;
};
struct BOOK *books;
int main(){
int i,noBooks;
noBooks=2;
books=malloc(sizeof(struct BOOK)*noBooks);
books[0].year=1986;
strcpy(books[0].name,"MartinEden");
strcpy(books[0].author,"JackLondon");
//asking user to give values
scanf("%d",&books[1].year);
scanf("%s",&books[1].name);
scanf("%s",books[1].author);
printf("%d %s %s\n",books[0].year,books[0].author,books[0].name);
printf("%d %s %s\n",books[1].year,books[1].author,books[1].name);
getch();
return 0;
}
我给1988 theidiotanddostoyevski
输出是
1986 JackLondon MartinEden
1988 dostoyevski theidiot
在scanf,在books[].name,我用&,在books[].author,我没用过,但还是一样。对于year,它不起作用。 &在结构上没用?
我是说这里
scanf("%d",&books[1].year);
scanf("%s",&books[1].name);
scanf("%s",books[1].author); //no & operator
char name[15];
char author[33];
在这里,我可以使用
char *name[15];
char *author[33];
没有任何变化。为什么我看不出区别?
【问题讨论】:
-
你到底想做什么?
-
我试图理解为什么 & 和 * 运算符不会改变任何东西。
-
&books[1].name和books[1].name产生相同的地址(这就是为什么它可以使用和不使用&),尽管结果类型略有不同。
标签: c