C: problem with char*

前端 未结 7 465
遥遥无期
遥遥无期 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 08:09

    This works if I set the return type to void:

    /*
     * code.c
     *
     * TASK
     *      Reverse a string by reversing pointers. Function should use return
     *      type char* and use a char* parameter as input.
     */
    #include 
    #include 
    #define STRMAX 51
    
    void reverse(char* sPhrase[]);
    
    int main() {
        char sPhrase[STRMAX];
        char* sPPhrase[STRMAX];
        char* sPReverse[STRMAX];
        int iCntr;
    
        printf("Enter string (max. 50 chars): ");
        gets(sPhrase);
    
        for (iCntr = 0; iCntr < strlen(sPhrase); iCntr++) {
            sPPhrase[iCntr] = &sPhrase[iCntr];
        }
    
        reverse(sPPhrase);          // Disabled return type char*
    
        return 0;
    }
    
    void reverse(char* sPPhrase[]) {
        char* sPOutput[STRMAX];
        int iCnt = 0, iCntRev;
    
        for (iCntRev = strlen(*sPPhrase)-2; iCntRev >= 0; iCntRev--) {
            sPOutput[iCnt] = sPPhrase[iCntRev];
            iCnt++;
        }
    
        *sPOutput[iCnt] = '\0';      // Don't forget to close the string
    
        // return sPOutput;     // Disabled return type char*
    }
    

    As soon as I get the return types back into play, things go wrong. sPReverse = reverse(sPPhrase); still returns the incompatible types in assignment error even though the return type is the same as the type of the variable I'm storing the returned pointer array in.

提交回复
热议问题