Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The parameters does not match up with the variable names.</p> <ul> <li>sPhrase is declared as an array of chars.</li> <li>The function <code>reverse</code> has a parameter <code>sPhrase</code> which is a pointer to an array of chars</li> </ul> <p>This explains why there is a 'type mismatch'. I have included a fixed version, try that.</p> <pre> char* reverse(char *sPhrase); int main() { char sPhrase[STRMAX]; char sReverse[STRMAX]; printf("Enter string (max. 50 chars): "); gets(sPhrase); sReverse = reverse(sPhrase); return 0; } char* reverse(char* sPhrase) { /* <strike>char* sOutput[STRMAX];</strike>*/ static char sOutput[STRMAX]; int iCnt = 0, iCntRev; for (iCntRev = strlen(sPhrase)-2; iCntRev >= 0; iCntRev--) { sPhrase[iCnt] = sOutput[iCntRev]; iCnt++; } sPhrase[iCnt] = '\0'; // Don't forget to close the string return sOutput; } </pre> <p>The compiler is pretty smart to check for 'incompatible' types. C is <strike>strict</strike> very relaxed when it comes to this kind of thing, so the onus is on you on making sure the declarations match up with the definitions in order for a successful compile.</p> <p>I have also corrected the problem with using the <code>sOutput</code> variable within the <code>reverse</code> function, and changed it to a <code>static</code> as the variable will go out of scope and will end up with garbage, by prefixing the keyword <code>static</code> will ensure that the variable remains in scope.</p> <p><strong>Edit:</strong> For some reason, I added the strike tag to strike out the line but others are seeing it somehow...???</p> <p>On closer inspection, the code does not work, I have fixed it, taking into account of other people's inputs and consideration, note I have used <code>fgets</code> instead of the evil <code>gets</code> function as that is a breeding ground for buffer overflows, also changed the function signature to use the <code>const</code> keyword for the parameter.</p> <pre> char * reverse(const char *sPhrase); int main() { char sPhrase[STRMAX]; char *sReverse; printf("Enter string (max. 50 chars): "); fgets(sPhrase, STRMAX - 1, stdin); sReverse = reverse(sPhrase); printf("Reversed string: %s\n", sReverse); return 0; } char *reverse(const char* sPhrase) { char* sOutput; int iCnt = 0, iCntRev; sOutput = (char *)malloc((STRMAX * sizeof(char)) + 1); if (sOutput){ for (iCntRev = strlen(sPhrase) - 1; iCntRev &gt;= 0; iCntRev--) { *sOutput++ = sPhrase[iCntRev]; iCnt++; } *sOutput++ = '\0'; } return (sOutput - iCnt); } </pre> <p>Here's an excellent <a href="http://home.netcom.com/~tjensen/ptr/pointers.htm" rel="nofollow noreferrer">tutorial</a> on pointers.</p> <p>Hope this helps, Best regards, Tom.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload