-2

I wanna compare an element of a string with a char, what's wrong? i get segmentation fault.

i wanna go trough a string, copyng the part of string until an element of string, when the element=",", but i'm making something of wrong.

passing argument 1 of ‘strcmp’ makes pointer from integer without a cast

     char palavra[1000],linha[1000];
     int i;

     while(fgets(linha, sizeof(linha), df)!=NULL){

                i=0;
                strcopy(palavra,"0");                    

                while(strcmp(&linha[i],",")!=0){

                    strcpy(&palavra[i],&linha[i]);
                    i++;                    

                }
      printf("%s,",palavra);
      }

3 Answers3

1

You should not be using strcmp to compare individual char, it is meant for comparing full strings. If you just want to compare individual elements, you would just compare them directly: linha[i] == ','

Same applies for copying a char.

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
1

should be

           while(linha[i] != ','){

                palavra[i] = linha[i];
                i++;                    

            }

Although I am a bit suspicious of palavra[i] assignment since that overwrites the strcpy of 0 done before the loop

pm100
  • 48,078
  • 23
  • 82
  • 145
-1
  1. You are passing address, not value &linha[i]
  2. to use strcmp(), both strings should be null terminated(\0)

So, use if as

for( i=0; i < len; i++)          
    if(linha[i]!=',')
Mesar ali
  • 1,832
  • 2
  • 16
  • 18