简体   繁体   English

如何将字符分配给指针?

[英]How would I assign a character to a pointer?

I'm making a radix converter and I wanted to use linked list to do this because I'm new to data structures.我正在制作一个基数转换器,我想使用链表来做到这一点,因为我是数据结构的新手。 When I assign my character to the pointer the compiler give a "error: assignment to expression with array type" error.当我将我的字符分配给指针时,编译器会给出“错误:赋值给数组类型的表达式”错误。

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

So conversion is a char * ;所以conversion是一个char * ; it's value when not using an index (within [] ) is a pointer at the beginning of the character array.不使用索引时的值(在[]内)是字符数组开头的指针。

If temp->bin_num is also char * you can pass a pointer to a specific position into the conversion array using:如果temp->bin_num也是char *您可以使用以下方法将指向特定 position 的指针传递到conversion数组中:

temp->bin_num = conversion + num;

Be aware though that you will now have a pointer to the remainder of the character array.请注意,您现在将拥有一个指向字符数组其余部分的指针。 That pointer will have to be de-referenced on use, ex: printf("%c\n", *temp->bin_num);该指针必须在使用时取消引用,例如: printf("%c\n", *temp->bin_num); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM