链接列表中的char,char *之间的差异

时间:2015-09-24 11:45:09

标签: c linked-list char char-pointer

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

所以它必须按照我在打印函数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; 
}  

2 个答案:

答案 0 :(得分:1)

您已声明名称并将其作为单个字符

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;
};

然后将函数的参数复制到struct

中的字段

e.g。

strcpy(ptr->name,name);
strcpy(ptr->lastn,lastn);

答案 1 :(得分:0)

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

%s期望char *不是char,因为namelastn都是char变量。

要存储您应该更喜欢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));