c语言,链表头插法,结构体中的char数组如何赋值
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"introprog_structs_lists...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "introprog_structs_lists_input.h"
#define MAX_STR 255
typedef struct _element{
char title[255];
char author[255];
int year;
long long int isbn;
struct _element *next;
} element;
typedef struct _list {
element *first;
int count;
} list;
element *insert_at_begin(element *first, element *new_elem) {
new_elem->next = first;
first = new_elem;
return first;
}
element *construct_element(char *title, char* author, int year, long long int isbn) {
element *new_elem = malloc(sizeof(element));
new_elem->title = *title;//报错的是这里上下两行,incompatible types when assigning to type 'char[255]' from type 'char *'
new_elem->author = author;//
new_elem->year = year;
new_elem->isbn = isbn;
return new_elem;
}
void free_list(list *alist) {
free(alist);
}
void read_list(char* filename, list *alist) {
element* new_elem;
char* title;
char* author;
int year;
long long int isbn;
read_line_context ctx;
open_file(&ctx, filename);
while(read_line(&ctx, &title, &author, &year, &isbn) == 0) {
new_elem = construct_element(title, author, year, isbn);
alist->first = insert_at_begin(alist->first, new_elem);
alist->count++;
}
}
list* construct_list() {
list *alist = malloc(sizeof(list));
alist->first = NULL;
alist->count = 0;
return alist;
}
void print_list(list *alist) {
printf("Meine Bibliothek\n================\n\n");
int counter = 1;
element *elem = alist->first;
while (elem != NULL) {
printf("Buch %d\n", counter);
printf("\tTitel: %s\n", elem->title);
printf("\tAutor: %s\n", elem->author);
printf("\tJahr: %d\n", elem->year);
printf("\tISBN: %lld\n", elem->isbn);
elem = elem->next;
counter++;
}
}
int main(int argc, char** argv) {
list *alist = construct_list();
read_list(argc>1?argv[1]:"buecherliste.txt", alist);
print_list(alist);
free_list(alist);
return 0;
}
*title *author返回的是英文书名和英文作者名
错误的在文中//标出了,试过new_elem->title[0] = *title;这样就是一个英文书名的首字母,而不是全部;new_elem->title[255] = *title;则是返回为空了。怎么样才能使new_elem返回全部的英文书名?
代码可能有点多,我自己都反复看过了,实在是找不到怎么解决,求助各位大神。。
另外读取文件的函数是老师给的,应该是没有问题的,如果需要再贴 展开
#include <stdlib.h>
#include <string.h>
#include "introprog_structs_lists_input.h"
#define MAX_STR 255
typedef struct _element{
char title[255];
char author[255];
int year;
long long int isbn;
struct _element *next;
} element;
typedef struct _list {
element *first;
int count;
} list;
element *insert_at_begin(element *first, element *new_elem) {
new_elem->next = first;
first = new_elem;
return first;
}
element *construct_element(char *title, char* author, int year, long long int isbn) {
element *new_elem = malloc(sizeof(element));
new_elem->title = *title;//报错的是这里上下两行,incompatible types when assigning to type 'char[255]' from type 'char *'
new_elem->author = author;//
new_elem->year = year;
new_elem->isbn = isbn;
return new_elem;
}
void free_list(list *alist) {
free(alist);
}
void read_list(char* filename, list *alist) {
element* new_elem;
char* title;
char* author;
int year;
long long int isbn;
read_line_context ctx;
open_file(&ctx, filename);
while(read_line(&ctx, &title, &author, &year, &isbn) == 0) {
new_elem = construct_element(title, author, year, isbn);
alist->first = insert_at_begin(alist->first, new_elem);
alist->count++;
}
}
list* construct_list() {
list *alist = malloc(sizeof(list));
alist->first = NULL;
alist->count = 0;
return alist;
}
void print_list(list *alist) {
printf("Meine Bibliothek\n================\n\n");
int counter = 1;
element *elem = alist->first;
while (elem != NULL) {
printf("Buch %d\n", counter);
printf("\tTitel: %s\n", elem->title);
printf("\tAutor: %s\n", elem->author);
printf("\tJahr: %d\n", elem->year);
printf("\tISBN: %lld\n", elem->isbn);
elem = elem->next;
counter++;
}
}
int main(int argc, char** argv) {
list *alist = construct_list();
read_list(argc>1?argv[1]:"buecherliste.txt", alist);
print_list(alist);
free_list(alist);
return 0;
}
*title *author返回的是英文书名和英文作者名
错误的在文中//标出了,试过new_elem->title[0] = *title;这样就是一个英文书名的首字母,而不是全部;new_elem->title[255] = *title;则是返回为空了。怎么样才能使new_elem返回全部的英文书名?
代码可能有点多,我自己都反复看过了,实在是找不到怎么解决,求助各位大神。。
另外读取文件的函数是老师给的,应该是没有问题的,如果需要再贴 展开
2个回答
展开全部
new_elem->title = *title;
//改为:
strcpy(new_elem->title,title);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询