简体   繁体   中英

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. Any other way of doing it??I want to store the strings present in 'pre' into an array each time it's generated..

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 :

 char **word; //and allocated accordingly

If you want word to just be a copy of pre, you should have something more like

 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 .

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. Instead,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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