如何将字符分配给指针?

时间:2019-10-04 15:24:19

标签: c pointers linked-list

我正在制作一个基数转换器,我想使用链接列表来执行此操作,因为我是数据结构的新手。当我将我的字符分配给指针时,编译器会给出“错误:分配给具有数组类型的表达式”错误。

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

1 个答案:

答案 0 :(得分:0)

所以conversionchar *;不使用索引(在[]中)时的值是字符数组开头的指针。

如果temp->bin_num也是char *,则可以使用以下方法将指向特定位置的指针传递到conversion数组中:

temp->bin_num = conversion + num;

请注意,尽管现在您将拥有一个指向字符数组其余部分的指针。该指针在使用时必须被取消引用,例如:printf("%c\n", *temp->bin_num);

相关问题