简体   繁体   English

将字符指针分配给字符指针数组

[英]Assigning character pointer to a character pointer array

In this below code I wan to copy the string in 'pre' to array 'word' so that I can print the array 'word' later but it's showing NONPORTABLE CONVERSION error.. Tried doing it using strcpy() but it dint work. 在下面的这段代码中,我希望将字符串“ pre”中的字符串复制到数组“ word”中,以便稍后可以打印数组“ word”,但它显示了NONPORTABLE CONVERSION错误。 Any other way of doing it??I want to store the strings present in 'pre' into an array each time it's generated.. 还有其他方法吗?我想在每次生成时将“ pre”中存在的字符串存储到数组中。

void print(char *pre,struct dic * root,int depth)
{
 int i=0,flag=0,int j=0;
 char *word;
for(;i<27;i++)
{
  if(root->node[i])
  {
    pre[depth]='a'+i;
    flag=1;
    print(pre,root->node[i],depth+1);
    pre[depth]=0;
  }
}
if(flag == 0)
{
   pre[depth]=0;
   printf("\n%s\n",pre);
//j is declared globally

***word[j]=pre;***


 //printf("\nWord[%d]=%s\n",j,word[j]);
 }
}

Thank you.. 谢谢..

If you want word to be an array of strings, then it should be declared as : 如果您希望word为字符串数组,则应将其声明为:

 char **word; //and allocated accordingly

If you want word to just be a copy of pre, you should have something more like 如果您希望单词只是pre的副本,则应该有一些类似的内容

 word[j] = pre[j]; // in a loop, but using strcpy or strncpy would be just as good...

If I understand the question right... 如果我理解正确的问题...

Your NONPORTABLE CONVERSION error is because 您的“非便携式转换”错误是因为

word[j]=pre;

is trying to assign a char* to a char . 试图到指定char*一个char

You didn't say what didn't work about your strcpy() , but I'm assuming, given the code shown, that you hadn't allocated any memory for char *word , and were trying to copy into NULL. 您没有说出什么对您的strcpy()无效,但是我假定,根据显示的代码,您没有为char *word分配任何内存,而是试图将其复制为NULL。 Instead, 代替,

  word=(char*)malloc(strlen(pre)+1);
  if (word) strcpy(word,pre);

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

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