C语言 链表中字符、字符*之间的差异



我创建了一个保存 int 和 char 类型数据的链表。一个函数将数据添加到列表中,另一个函数将其打印出来。当我只打印 int 类型时,我没有问题,但是当我尝试打印字符类型时,程序崩溃了。

所以它必须按照我在打印函数 print_list(( 中定义 char* 的方式进行操作。

更具体地说,我的问题在 print_list(( 中:

printf("n [%s] n", ptr -> name);
printf("n [%s] n", ptr -> lastn);

所以我的实际代码是(收到 0 个错误和 0 个警告,但程序崩溃(:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
// Creating structure for node 
struct test_struct 
{ 
    int val;         // val is member id number 
    char name; 
    char lastn; 
    int age; 
    struct test_struct *next; 
}; 
// declaring global head and curr pointers 
struct test_struct *head = NULL; 
struct test_struct *curr = NULL; 
// creating a list 
struct test_struct* create_list(int val, char* name, char* lastn, int age) 
{ 
    printf("n creating list with head node as [%d] [%s] [%s] [%d] n", val, name, lastn, age); 
    struct test_struct *ptr = malloc(sizeof(struct test_struct)); // creating list 
    if(NULL == ptr) { 
        printf("n Node creation failed n"); 
        return NULL; 
    } 
    ptr->val = val; 
    ptr->name = *name; 
    ptr->lastn = *lastn; 
    ptr->age = age; 
    ptr->next = NULL; 
    head = curr = ptr; 
    return ptr; 
}
// add member to list 
struct test_struct* add_to_list(int val, char *name, char *lastn, int age, bool add_to_end) 
{ 
    if(NULL == head) { 
        return (create_list(val, name, lastn, age)); 
    }      
    if(add_to_end) { 
        printf("n Adding node to end of list with data [%d] [%s] [%s] [%d] n",  val, name, lastn, age); 
    } else { 
        printf("n Adding node to beginning of list with data [%d] [%s] [%s] [%d] n", val, name, lastn, age); 
    } 
    struct test_struct *ptr = malloc(sizeof(struct test_struct)); 
    if(NULL == ptr) { 
        printf("n Node creation failed n"); 
        return NULL; 
    } 
    ptr->val = val; 
    ptr->name = *name; 
    ptr->lastn = *lastn; 
    ptr->age = age; 
    ptr->next = NULL; 
    if (add_to_end) { 
        curr-> next = ptr; 
        curr = ptr; 
    } else { 
        ptr -> next = head; 
        head = ptr; 
    } 
    return ptr; 
} 
//printing the list 
void print_list(void) 
{ 
    struct test_struct *ptr = head; 
    printf("n -----Printing list Start----- n"); 
    while(ptr != NULL) { 
        printf("n [%d] n", ptr -> val); 
        printf("n [%s] n", ptr -> name); 
        printf("n [%s] n", ptr -> lastn); 
        printf("n [%d] n", ptr -> age); 
        ptr = ptr->next; 
    } 
    printf("n -----Printing list end---- n"); 
    return; 
}   
// main function 
int main(void) 
{ 
    struct test_struct *ptr = NULL; 
    // for adding member to list 
    add_to_list(123, "william", "shakespeare", 30, true); 
    add_to_list(124, "william", "gibson", 35, true); 
    add_to_list(125, "chuck", "palahniuk", 40, true); 
    add_to_list(126, "mario", "puzio", 50, true); 
    add_to_list(127, "umberto", "eco", 60, true); 
    add_to_list(128, "ezra", "pound", 125, true); 
    print_list(); 
    return 0; 
}  

您已将名称和 lastn 声明为单个字符

struct test_struct
{
int val;         // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};

您需要将它们声明为固定大小的数组或指针,这些指针指向分配的空间来保存字符串。字符串是以 \0 结尾的一系列字符。

struct test_struct
{
int val;         // val is member id number
char name[MAXLEN];
char lastn[MAXLEN];
int age;
struct test_struct *next;
};

然后将函数的参数复制到结构中的字段

例如

strcpy(ptr->name,name);
strcpy(ptr->lastn,lastn);
printf("n [%s] n", ptr -> name);
printf("n [%s] n", ptr -> lastn);

%s预计char *不会char,因为namelastn都是字符变量。

并且要存储一个人的姓名,您应该更喜欢char array因为单身char variable无法存储它。因此,将它们声明为 char arrays

例-

struct test_struct
{
  int val;         // val is member id number
  char name[20];         // or any desired length to store a name 
  char lastn[20];        // similar as for name
  int age;
  struct test_struct *next;
};

然后使用strncpy复制其中的数据 -

ptr->name = *name;          // strncpy(ptr->name,name,strlen(name));
ptr->lastn = *lastn;        // strncpy(ptr->lastn,lastn,strlen(lastn));

相关内容

  • 没有找到相关文章

最新更新