【发布时间】:2019-10-04 15:24:19
【问题描述】:
我正在制作一个基数转换器,我想使用链表来执行此操作,因为我是数据结构的新手。当我将我的字符分配给指针时,编译器会给出“错误:赋值给数组类型的表达式”错误。
struct node
{
char bin_num[25];
struct node *next;
};
char conversion[50] = "0123456789abcdefghijklmnopqrstuvwxyz!@#$/^&*";
typedef struct node NODE;
NODE *head = NULL;
int insert (int num){
NODE *temp;
temp = (NODE *) malloc (sizeof (NODE));
temp->bin_num = conversion[num];//Need help with this line
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
temp->next = head;
head = temp;
}
}
//This is not the entire code
【问题讨论】:
-
temp->bin_num = conversion[num];你想在这里做什么? LHS 的类型是char [25],而 RHS 很可能是char...它们肯定不兼容。 -
bin_num是一个数组,而不是一个指针。目前尚不清楚为什么它是一个数组而不仅仅是一个char,或者你想用那条线做什么。 -
temp->bin_num = conversion[num];也许如果bin_num被声明为实际指针而不是数组...另外,为什么需要它作为指针? -
@Yathartha 你想只为数组 bin_num 分配一个字符还是数组转换的 num 个字符的子字符串?在第一种情况下,不清楚为什么要在 NODE 中声明整个字符数组而不是 char 类型的变量。
标签: c pointers linked-list