-1
main()
{
FILE *fin;
char line[50];
char exp[SIZE];
fin=fopen("prefix.txt","r");

if(fin==NULL)
{
     printf("\nFile Cannot be Opened\n");              
}
else
{
        printf("\nfile opened\n");

       while(fgets(line, sizeof(line), fin)!=NULL)
       {
              sscanf(line, "%s", exp);

              exp=delete_spaces(exp);
       }    
}
fclose(fin);
getch();   
}

char delete_spaces(char a[])
{
   int l,k=0,i=0;
   l=strlen(a);

   while(i<l)
   {
        if(a[i]==' ')
        i++ 
        else
        a[k++]=a[i++];    
   } 
   return a;    
}

After compiling this program I am getting error "Compatibility Type Error" In line containing "exp=delete_spaces(exp);" and I don't know how to remove it. Is there is some problem with array passing ?

alk
  • 69,737
  • 10
  • 105
  • 255
user39495
  • 151
  • 1
  • 9

1 Answers1

0

Here you are passing the array address to delete_spaces(char a[]) in call delete_spaces(exp) so you dont need to take the return value and assign back as

exp=delete_spaces(exp);

instead change function defination to,

void delete_spaces(char a[]);

and function call to

delete_spaces(exp);

And also put the definition or prototype of function before main(). And remove the return statement from delete_spaces() definition.

and the code for delete_spaces() will be

void delete_spaces( char a[])
{
    int l,k=0,i=0;
   l=strlen(a);

   while(i<l)
   {
        if(a[i]==' ')
        i++; 
        else
        a[k++]=a[i++];    
   } 
   a[k]='\0';
}
LearningC
  • 3,182
  • 1
  • 12
  • 19
  • @user39495 ok. i made change to answer check it. it may help – LearningC Mar 20 '14 at 18:05
  • yup it is working but earlier why it was not working – user39495 Mar 20 '14 at 18:11
  • @user39495 It is not working because exp is a char array and in C char[] and char * are not same. read [this](http://stackoverflow.com/questions/16677858/char-and-char-arr-difference-c-c) or related questions to get some idea. – LearningC Mar 20 '14 at 18:18
  • above function delete_spaces is taking any array and deleting spaces between characters, will the above code will work ??? – user39495 Mar 20 '14 at 18:47
  • @user39495 ya it works but add a[k]='\0'. check the code i added to answer.it may help you – LearningC Mar 20 '14 at 18:56
  • yup i agree but as you can see that I am taking expression value from file and if given expression is containing spaces then it wont take full expression. – user39495 Mar 20 '14 at 19:01
  • i want to take an expression like * + a b / c d – user39495 Mar 20 '14 at 19:07
  • @user39495 fgets stops reading when either (sizeof(line)-1) characters are read or the newline character is read, or the end-of-file is reached, whichever comes first. therefore it will read such expressions from file since fgets() reads spaces. – LearningC Mar 20 '14 at 19:17
  • then what should i use to take a string from file which is containing spaces ?? – user39495 Mar 20 '14 at 19:22
  • @user39495 fgets() reads * + a b / c d this input. since it is in single line provided line[] can hold it. – LearningC Mar 20 '14 at 19:26