链表中 char、char* 的区别
Difference between char, char* inside Linked Lists
我创建了一个包含 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;
}
您已将姓名和姓氏声明为单个字符
struct test_struct
{
int val; // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};
您需要将它们声明为固定大小的数组或指向已分配 space 以保存字符串的指针。字符串是以 \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
因为 name
和 lastn
都是 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));
我创建了一个包含 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;
}
您已将姓名和姓氏声明为单个字符
struct test_struct
{
int val; // val is member id number
char name;
char lastn;
int age;
struct test_struct *next;
};
您需要将它们声明为固定大小的数组或指向已分配 space 以保存字符串的指针。字符串是以 \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
因为 name
和 lastn
都是 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));