Search code examples
cpointersfunction-parameter

C2100 Error: Illegal Indirection


I'm not understanding this error (C2100: illegal indirection). There are three instances I've marked-all near the bottom. I've looked online and I know it has to do with my pointers, but after 8 hours on this, I'm completely lost. There may be some other errors in here too, but I can't even tell because I can't get it to compile. Please help & I'd love an explanation I can understand so I can figure out what I'm doing wrong for the future.

// INCLUDE FILES
#include <stdio.h>
#include <string.h>

// PROGRAM CONSTANTS
#define     MAX_MSG_LEN     81      // Maximum Message Length (Including /0 Character)

// FUNCTION PROTOTYPES
void printLength(int, int);         // Function to Validate & Print Length of String
void printString(int, char);        // Function to Print the String in Reverse
void writeToFile(int, char);

// GLOBAL VARIABLES
char input[MAX_MSG_LEN];            // Input String
int maxLength = MAX_MSG_LEN - 1;    // Actual String Length (Not Including /0 Character)
char *ptr = input;                  // Character Pointer to String
int length = 0;                     // Length of Current String
int lcv = 0;                        // Loop Control Variable


void main()
{
    FILE *ifp;
    ifp = fopen("reverseString.txt", "w");

    printf("\n\nEnter a String Between 1 and %d Characters: ", maxLength);  // Prompts User to Enter a String Less Than 80
    gets(input);        // Receives the Inputted String from the User   

    length = strlen(input);     // Counts the Length of the Inputted String & Assigns the Number to the "length" Variable

    printLength(length, maxLength);
    printString(length, *ptr);
    writeToFile(length, *ptr);
}

void printLength(int length, int maxLength)
{
    if(length > maxLength)
    {
        printf("\n\nThe Maximum Length of %d Characters was Exceeded!", maxLength);
        printf("\nProgram Terminated...\n\n");
        exit(0);
    }

    printf("\n\nThe Length of the Input String was: %d\n", length);     // Prints the Length of the Inputted String
}

void printString(int length, char ptr)
{   
    for(; lcv < length; lcv++)
    {
        ptr++;
    }

    length = lcv;
    printf("\nThe String in Reverse: ");

    for(ptr--; length > 0; length--)
    {
        printf("%c", *ptr);     // HERE IS ONE INSTANCE OF C2100
        *ptr--;                 // HERE IS ONE INSTANCE OF C2100
    }

    printf("\n\n");
    return;
}

void writeToFile(int length, char ptr)
{
    FILE *ifp;
    ifp = fopen("reverseString.txt", "w");
    fprintf(ifp, "%c", *ptr);   // HERE IS ONE INSTANCE OF C2100
    fclose(ifp);
}

Solution

  • In your code, your syntax is wrong. You need to change the declaration and definition(s)

      void printString(int, char);
    

    to

      void printString(int, char*);
    

    and call it like

     printString(length, ptr);
    

    Same goes for writeToFile() function.

    Otherwise, with the present code, in printString() and writeToFile() functions, with a definition like char ptr, ptr is not a pointer type that you can dereference.

    That said,

    • Never use gets(), it suffers from buffer overflow issues, use fgets() instead.

    • Always check the return value from fopen() before using the returned pointer to ensure the success of the call.

    • To conform to the standards, void main() should be int main(void), at least.