以下代码的第 9 行生成未定义的行为。这是因为它在全球范围title1[]
之外吗?main()
还是因为我缺少其他东西?
1. char title1[]="The Name of the Rose";
2. Book book1={title1,900,0};
3. int main(){
4. Book book2={"Foucault's Pendulum",1000,0};
5. Book* book3=(Book*)malloc(sizeof(Book));
6. *book3=book2;
7. book1.next=&book2;
8. book2.next=book3;
9. book1.title[0]='B';
10. book2.title[0]='A';
11. {
12. Book list[2];
13. list[0]=book1;
14. list[1]=book2;
15. list[1].next->next=&book2;
16. {
17. Book* p=&list[0];
18. while (p!=0) {
19. p=p->next;
20. }
21. }
22. return 0;
23.}
编辑:
添加了书籍定义:
;struct Book
typedef struct Book {
; char* title
int pages;
; struct Book* next
;} Book