C:从链表中检索字符值
C: Retrieving char value from linked list
我一直在篡改存储 char 数组类型值的链表。我遇到的问题是检索要存储在另一个用于显示的字符中的值。
我试过使用(示例):
char display*;
node * data = <method to retrieve data as node *>
strcpy(display, data->value);
这给了我警告:"passing argument 2 of ‘strcpy’ makes pointer from integer without a cast" 指向数据。
sprintf(display, "%s", data->value);
这给了我警告:
"format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’"
指向数据->值并建议我改用 %d。
目前如果我编译显示第一个字母但其余字母很小squares/characters。
我做错了什么?
谢谢
[编辑]
这是结构的初始化:
typedef struct node_list {
char value;
struct node_list * next;
} node;
这是推送:
void push(char *value) {
node_list* n = (node*)malloc(sizeof(node));
n->value = *value;
n->next = head;
head = n;
}
[编辑 2]
已修复抱歉浪费时间。我遵循的指南显然有错误,因为它用 char 值而不是 char 值 [10] 声明结构(例如。)
它的发生是因为结构 "node" 的成员 "value" 可能是一个 int(整数)类型。
Post 所有代码以获取更多详细信息。
转换 char "%s" 需要一个 char (*char) 的指针并且你的结构的成员 "value" 是 char 类型(所有 char 类型都可以转换为 int 类型,因为所有 char 都被表示通过 int 值,正是我之前所说的)。
此外,strcpy函数不适用于char类型。 strcpy函数原型为:
char * strcpy ( char * destination, const char * source );
在你的成员"value"中,你需要只保存char还是char数组?
如果您需要一个字符数组,并且需要将您的结构更改为:
typedef struct node node_list;
struct node {
char *value;
node_list *next;
};
并且在为节点指针分配内存并使用 strcpy 函数后,您为 "value" 成员分配了内存,如下所示:
void push(char **value) {
node_list* n = (node_list*)malloc(sizeof(node_list));
n->value = (char *) malloc(sizeof(char));
strcpy(n->value, *value);
printf("Output: %s\n", n->value);
free(n->value);
free(n);
}
主程序示例:
int main(int argc, char **argv)
{
char *palavra = (char *) malloc(sizeof(char));
printf("Input: ");
scanf("%[^\n]s", palavra);
push(&palavra);
free(palavra);
return (0);
}
您正在向 push
函数传递一个字符串,但仅将其第一个字符存储在您的列表节点中。如果要存储整个字符串,则需要进行一些更改。
首先,您需要将 value
更改为结构中的 char *
。
typedef struct node_list {
char *value;
struct node_list * next;
} node;
并且在您的 push
函数中,您需要对字符串进行 复制 以将其存储在节点中。
n->value=malloc(strlen(value)+1); // allocate memory to store the new string plus 1 extra for the null terminator
strcpy(n->value, value); // copy the string
你需要记住 free
以后销毁列表时需要额外的内存。
这仍然不能完全修复您最初发布的代码,因为 display
是一个未初始化的指针,因此您不能只对它 strcpy
或 sprintf
。您要么需要像我在 push
上面所做的那样为其分配足够的内存,要么您可以将值分配给 display
因为根据名称判断,您只会显示它.
或者甚至根本不用 display
而是直接使用节点中的 value
。以下将正常工作...
node * data = <method to retrieve data as node *>
printf("Node value is %s\n",data->value);
我一直在篡改存储 char 数组类型值的链表。我遇到的问题是检索要存储在另一个用于显示的字符中的值。
我试过使用(示例):
char display*;
node * data = <method to retrieve data as node *>
strcpy(display, data->value);
这给了我警告:"passing argument 2 of ‘strcpy’ makes pointer from integer without a cast" 指向数据。
sprintf(display, "%s", data->value);
这给了我警告:
"format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘int’"
指向数据->值并建议我改用 %d。
目前如果我编译显示第一个字母但其余字母很小squares/characters。
我做错了什么?
谢谢
[编辑]
这是结构的初始化:
typedef struct node_list {
char value;
struct node_list * next;
} node;
这是推送:
void push(char *value) {
node_list* n = (node*)malloc(sizeof(node));
n->value = *value;
n->next = head;
head = n;
}
[编辑 2]
已修复抱歉浪费时间。我遵循的指南显然有错误,因为它用 char 值而不是 char 值 [10] 声明结构(例如。)
它的发生是因为结构 "node" 的成员 "value" 可能是一个 int(整数)类型。
Post 所有代码以获取更多详细信息。
转换 char "%s" 需要一个 char (*char) 的指针并且你的结构的成员 "value" 是 char 类型(所有 char 类型都可以转换为 int 类型,因为所有 char 都被表示通过 int 值,正是我之前所说的)。
此外,strcpy函数不适用于char类型。 strcpy函数原型为: char * strcpy ( char * destination, const char * source );
在你的成员"value"中,你需要只保存char还是char数组?
如果您需要一个字符数组,并且需要将您的结构更改为:
typedef struct node node_list;
struct node {
char *value;
node_list *next;
};
并且在为节点指针分配内存并使用 strcpy 函数后,您为 "value" 成员分配了内存,如下所示:
void push(char **value) {
node_list* n = (node_list*)malloc(sizeof(node_list));
n->value = (char *) malloc(sizeof(char));
strcpy(n->value, *value);
printf("Output: %s\n", n->value);
free(n->value);
free(n);
}
主程序示例:
int main(int argc, char **argv)
{
char *palavra = (char *) malloc(sizeof(char));
printf("Input: ");
scanf("%[^\n]s", palavra);
push(&palavra);
free(palavra);
return (0);
}
您正在向 push
函数传递一个字符串,但仅将其第一个字符存储在您的列表节点中。如果要存储整个字符串,则需要进行一些更改。
首先,您需要将 value
更改为结构中的 char *
。
typedef struct node_list {
char *value;
struct node_list * next;
} node;
并且在您的 push
函数中,您需要对字符串进行 复制 以将其存储在节点中。
n->value=malloc(strlen(value)+1); // allocate memory to store the new string plus 1 extra for the null terminator
strcpy(n->value, value); // copy the string
你需要记住 free
以后销毁列表时需要额外的内存。
这仍然不能完全修复您最初发布的代码,因为 display
是一个未初始化的指针,因此您不能只对它 strcpy
或 sprintf
。您要么需要像我在 push
上面所做的那样为其分配足够的内存,要么您可以将值分配给 display
因为根据名称判断,您只会显示它.
或者甚至根本不用 display
而是直接使用节点中的 value
。以下将正常工作...
node * data = <method to retrieve data as node *>
printf("Node value is %s\n",data->value);