3

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);
}
Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
musikluver2003
  • 165
  • 1
  • 9
  • When you fix the error in `printString` then note that you don't really need the first loop, you can just do `ptr += length`. You also don't need the assignment to `length`, and because of that you don't need the global variable `lcv` (which is good, global variables should be avoided). – Some programmer dude Jan 25 '16 at 07:45
  • 1
    You are sending the content of ptr from the main, which is basically the string (`char* ptr`). Your function accepts it as a single character( `char ptr` ) and then your logic tries to get the content of the character in variable ptr ( `*ptr`). No wonder it fails. – Nithish Jan 25 '16 at 07:55

2 Answers2

3

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.

Sourav Ghosh
  • 127,934
  • 16
  • 167
  • 234
  • 1
    Works great now! Thanks for explaining as well - I was pretty lost! – musikluver2003 Jan 25 '16 at 08:00
  • @musikluver2003 Welcome :) – Sourav Ghosh Jan 25 '16 at 08:01
  • 1
    `void main()` is allowed by the standard in many cases, there are not many restrictions of main's format at all, as far as the standard goes. See [this](http://stackoverflow.com/a/31263079/584518). For example, Visual Studio allows `void main()` as impl. defined behavior and that's fine because they have documented it. Just note that it is the compiler that decides the format of main, not the programmer. – Lundin Jan 25 '16 at 08:03
  • @Lundin thanks sir, I know of that post already. Just for most of the cases, the hosted environment, `int main(void)` seems suitable, in case we don't intend to use command line args. `void main()` is also acceptable, but `int main(void)` is preferred. Please correct me if i'm wrong. thanks – Sourav Ghosh Jan 25 '16 at 08:07
  • @SouravGhosh There's no preference. C99 and C11 just say "or otherwise in some implementation-defined manner". But of course if you write a program which relies on an implementation-defined form of main, the program will not be portable. As for freestanding systems (which the OP may very well be), `void main()` is so very common that it is in practice always portable. – Lundin Jan 25 '16 at 08:28
2

I tried to compile your code on DevC++ IDE and I found out 3 issues with ur code. I would list down as follows.

1) Try changing the declarations of functions printString() and writeToFile() as Saurav Ghosh suggested.

2) include stdlib.h header to support exit() function as the definition of exit() is avaliable in stdlib.h

3)Change void main(){/* your code*/} to int main(){/* your code */}

I did the above changes and it successfully compiled on my machine.

SKD
  • 456
  • 1
  • 3
  • 16
Meraj Hussain
  • 185
  • 2
  • 18