我正在尝试打印一个字符链接列表,然后创建一个函数将字符串复制到一个新字符串上,然后反转它并打印出来。我很困惑为什么这只打印出整数,以及如何反转字符串。这是我的代码:
#include<stdlib.h>
#include<stdio.h>
struct list_el
{
char val;
struct list_el * next;
};
typedef struct list_el item;
int main()
{
item * curr, * head;
int i;
head = NULL;
for(i='i';i>='a';i--)
{
curr = (item *)malloc(sizeof(item));
curr->val = i;
curr->next = head;
head = curr;
}
head = NULL;
while(curr)
{
printf("%d\n", curr->val);
curr = curr->next ;
}
return 0;
}
最佳答案
%d
仅根据定义打印数值。如果您想打印其他内容,请使用不同的格式字符串(例如%c
)。
更好的是,根本不要使用 printf()
,而是考虑使用更简单的东西(putc()
?)
关于c - 为什么这不打印字符而只打印整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24666872/