A function to scan user input as string I know that multiple functions are already available. However, I thought of writing my own because I wanted to learn the logic (and also because I thought there wasn't enough confusion :P). Please review the function I wrote and suggest me efficient changes.

Without further ado, here I go:

scan(string,size)
char** string;
size_t size;
{
    string[0]=(char*)malloc(sizeof(char));
    char keystroke=' ';
    while((keystroke=getc(stdin))!='\n') {
        string[0][size++]=keystroke;
        string[0]=(char*)realloc(string[0],size+1);
    }
    string[0][size]='\0';
    return size;
}
Your code is horrible:


You don't check for end-of-file, which will lead to endless loops
You don't check for failure of malloc or realloc
You call realloc way too often, which makes the code slow for large lines of input
You cast the result of malloc
You use the outdated style of function definition from the 1980s
The variable string is not a string but points to an array of pointers to strings, which is confusing
The #include for size_t is missing
Using [0] instead of * is extremely confusing
There is no need to initialize keystroke to a space character
The parameter size is useless since the only possible value that makes sense for it is 0
The return type of the function is implicitly int, which is obsolete
The returned value is of type size_t, which doesn't fit into an int and additionally differs in signedness