0

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

2 Answers 2

1

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...
Sign up to request clarification or add additional context in comments.

Comments

0

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);

1 Comment

Ahh, I re-read (right after submitting) and saw the commented-out line at the bottom of your code. @Matthieu is right that static char **word; is what you want, with malloc()/realloc() as necessary for both the original array of strings, and for each string that you strcpy() into. Note that it should be static if you're building onto this array every time you call into the function (and if it's only needed within the function).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.