Code:
#include <stdio.h>
#include <stdlib.h>
/* Identifier clash, underscores are reserved,
** as are identifiers that start with str
*/
void _gets(char *str) {
char temp[1024];
/* temp is just that, temp. It won't exist when
** you return.
*/
str=temp;
}
/* Big no-no, your entire program is undefined
** starting here.
*/
void main() {
char *s;
_gets(s);
}
Let's rewrite this so that it's better. I'm assuming that you want to pass the function gets a char pointer which it will allocate memory for.
Code:
#include <stdio.h>
#include <stdlib.h>
static int gets_(char **st)
{
*st = malloc ( 1024 * sizeof **st );
if ( *st != NULL )
return EXIT_SUCCESS;
return EXIT_FAILURE;
}
int main ( void )
{
char *s = NULL;
if ( gets_(&s) == EXIT_SUCCESS )
free ( s );
return 0;
}
The names have been changed to protect the implementation, thorough testing of return values is performed so that bad thingsTM don't happen, and memory is freed at the end. This program is much better.
-Prelude