C: problem with char*

前端 未结 7 412
遥遥无期
遥遥无期 2021-01-22 07:38
/*
 * code.c
 *
 * TASK
 *      Reverse a string by reversing pointers. Function should use return
 *      type char* and use a char* parameter as input.
 */
#include &l         


        
7条回答
  •  悲&欢浪女
    2021-01-22 07:54

    The parameters does not match up with the variable names.

    • sPhrase is declared as an array of chars.
    • The function reverse has a parameter sPhrase which is a pointer to an array of chars

    This explains why there is a 'type mismatch'. I have included a fixed version, try that.

    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) {
        /* char* sOutput[STRMAX];*/
        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;
    }
    

    The compiler is pretty smart to check for 'incompatible' types. C is strict 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.

    I have also corrected the problem with using the sOutput variable within the reverse function, and changed it to a static as the variable will go out of scope and will end up with garbage, by prefixing the keyword static will ensure that the variable remains in scope.

    Edit: For some reason, I added the strike tag to strike out the line but others are seeing it somehow...???

    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 fgets instead of the evil gets function as that is a breeding ground for buffer overflows, also changed the function signature to use the const keyword for the parameter.

    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 >= 0; iCntRev--) {
                *sOutput++ = sPhrase[iCntRev];
                iCnt++;
            }
            *sOutput++ = '\0';
        }
        return (sOutput - iCnt);
    }
    

    Here's an excellent tutorial on pointers.

    Hope this helps, Best regards, Tom.

提交回复
热议问题