Search code examples
cmessage

Explanation of missing prototype error message for custom getln function in C


#include <stdio.h>
#include <stdlib.h>
#include <reg51.h>


void main (void) {
SCON = 0x52;       // serial port configuration
TMOD = 0x20;       
TCON = 0x40;
TH1 = 0xf3;        // 2403 baudrate @12mhz

printf("Hello World");
printf("Please enter some text: ");
scanf(getLine());

}

const char *getLine()
{

    char *line = NULL, *tmp = NULL;
    size_t size = 0, index = 0;
    int ch = EOF;

    while (ch) {
        ch = getc(stdin);


        if (ch == EOF || ch == '\n')
            ch = 0;


        if (size <= index) {
            size += CHUNK;
            tmp = realloc(line, size);
            if (!tmp) {
                free(line);
                line = NULL;
                break;
            }
            line = tmp;
        }


        line[index++] = ch;
    }

    return line;
}
free(str);

That's my code. I think I'm calling getln incorrectly. Is there a way to have the function accept input I can pass in from the user?

This compiler is an evaluation version but I believe it contains the libraries I need.

My goal is to accept a "string", or rather an array of chars and then manipulate its order as a skill test. I only have 2000 KB of memory available to write this.

I'm a bit of a novice when it comes to pointers and not being able to reference. Help or even just explanations are much appreciated.

I'm using a KEIL compiler.

When I select Program>Rebuilt All Target Files to check my errors I receive the following:

  • assembling STARTUP.A51... assembling XBANKING.A51... compiling Main.c... MAIN.C(14): warning C206: 'getln': missing function-prototype MAIN.C(14): error C214: illegal pointer conversion Target not created

Thank you,


Solution

  • You have correctly defined the function at the bottom, but the C compiler needs a list of functions, called prototypes, at the top. The prototype must include the function type, function name, and any arguments the function takes. For example:

    #include <stdio.h>
    
    void hello_world(); //This is the function prototype
    
    int main()
    {
        hello_world();
    }
    
    void hello_world()             //You did this part correctly, but C needs the 
    {                             //prototype at the top in order to see this as a 
        printf("Hello, world!\n"); //defined function
    }
    

    In your case, the prototype would simply be:

    const char *getLine(void);
    

    and then your program would run without any prototype errors. Cheers!