Pregunta

I'm not a C programmer but I need to get a simple POC for one of our applications that can be extended with C. I've got the code compiling by passing data straight through my module and my idea is to pass out the reverse of the character string passed in to prove my code has actually done something to the data.

I currently have this code to pass each input straight to the output:

INFA_CTSetData(outputGroupPorts[i], 
    INFA_CTGetDataStringM(inputGroupPorts[i]));

The INFA_CTGetDataStringMfunction is defined as:

char* INFA_CTGetDataStringM(INFA_CT_INPUTPORT_HANDLE dataHandle);

I have found a function to reverse a string here

char *mystrrev(char *s)
{
   char *start = s, *t = strchr(s, '\0'); /* point to end of string */
   /*
    * Swap the values at the beginning (pointed to by 's') and the end
    * (pointed to by 't'); 's' and 't' meet in the middle.
    */
   for ( --t/* skip terminating null character */; s < t; ++s, --t )
   {
      /* Just your run-of-the-mill swap here. */
      char temp = *s;
      *s = *t;
      *t = temp;
   }
   return start;
}

What I'm failing to do is pass the result of the call to INFA_CTGetDataStringM through mystrrev and on to INFA_CTSetData.

INFA_CTSetData(outputGroupPorts[i],
    mystrrev(INFA_CTGetDataStringM(inputGroupPorts[i])));

produces these errors at compilation

p_Reverse.c:155: error: conflicting types for 'mystrrev'
p_Reverse.c:145: error: previous implicit declaration of 'mystrrev' was here

Years if Visual Basic and C# have made my life too easy - can some one help me get this code working? I've blindly tried adding * and & to get different compiler errors - and now I'm stumped.

¿Fue útil?

Solución

p_Reverse.c:155: error: conflicting types for 'mystrrev'
p_Reverse.c:145: error: previous implicit declaration of 'mystrrev' was here

Implicit declaration is a real antifeature left over from the days of yore. You can enable warnings/errors (or C99 mode) to prevent it from hurting you.

On line 145, you have a call to mystrrev and your compiler has likely presumed that it was declared int mystrrev(int). Later, on line 155, where you define it to be char *mystrrev(char *), it conflicts with the "original" presumed to be int mystrrev(int).

Add a declaration of your function before anywhere it's used (towards the top of the file) like so:

char *mystrrev(char *val);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top