Search code examples
cfunctionfor-loopif-statementansi-c

error: expected expression before ‘char’


Excuse me , I have a problem when I compile my code, and i don't don't how to solve it because I don't know what it ask for.

This appears me in the terminal:

" usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/crt1.o: In the functión _start': /build/buildd/glibc-2.21/csu/../sysdeps/x86_64/start.S:114: reference tomain' without define collect2: error: ld returned 1 exit status "

I leave my code:

#include<stdio.h>
#include<string.h>
void calcular_vocal(char cadena[]){
   int l;
   char vocales[5]="a,e,i,o,u";
   int i, vector contadores[5];
   l=strlen(cadena);
   for(i=0;i<l;i++){
     if(cadena[i] =='a')
        cadena a++;
        vector contadores[0]++;
     if(cadena[i] =='e')
        cadena e++;
        vector contadores[1]++;;
     if(cadena[i] == 'i')
        cadena i++;
        vector contadores[2]++;
     if(cadena[i] =='o')
        cadena o++;
        vector contadores[3]++;
     if(cadena[i] =='u')
        cadena u++;
        vector contadores[4]++;
    for(i=0;i<5;i++){
      int max,pos;
      if(vector contadores[i]>max){
         max = vector contadores [i];
         pos = i;
      }
    }
     printf("The most repeated vocal is %c %d",vocales[pos],max);
   }
}
int main (void){
   char calcular_vocal(char[]);
}

Solution

  • Update

    It seems you are compiling the wrong file. See the following console commands and output (its german, but I think the message is sufficiently similar to yours):

    $ rm test1.c 
    $ touch test1.c
    $ gcc test1.c 
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In Funktion `_start':
    (.text+0x20): Nicht definierter Verweis auf `main'
    collect2: error: ld returned 1 exit status
    $ 
    

    Long story short: you compile the wrong file (probably an empty one) and for that reason you get this error message which is totally unrelated to any of your code.


    Original Answer

    I'm going to assume a few things about your code, in order to provide you an answer that results in a compilable code - whether the result of running the program means anything is up to you (to be honest, I intentionally suggest changes that won't give you the program you most probably want, since you need something to learn with).

    I start with your main function

    int main (void){
       char calcular_vocal(char[]);
    }
    

    What you do inside the main function, is writing a declaration of the calcular_vocal function, but with a different function signature than your actual implementation. Change it to void calcular_vocal(char[]); in order to have this part compilable.

    Now to the code inside the calcular_vocal function:

    The line char vocales[5]="a,e,i,o,u"; is not allowed, because the string size is to big for your array. Increase the array size to 9 or 10 in order to have enough space available: char vocales[9] = "a,e,i,o,u";

    Moving on to line int i, vector contadores[5];, assuming you want to have a variable named "vector contadores". You cannot have space in your variable names, so rename it to "vector_contadores" for all occurences in order to make this piece work.

    Moving on to all the if-statements. They are all the same, so I only write something about the first one. The code cadena a++; is invalid. You have multiple options to solve this. (1) erase the a from that code (cadena++;). (2) define integer variable for a and remove cadena from the code (a++;). (3) rewrite to cadena['a']++;. There are even more options to make this part compileable.

    The variables int max,pos; are defined inside the for-loop but are used outside. Move their definition to the top of the function, where you define your other variables. Also, initialize max with value -1.

    This should be all you need to compile this piece of code (unless I forgot something)