See that's what I thought, its uninitialized, but isn't it initialized with the return value of fgets?
Originally Posted by Prelude
see i had this code before
and it works fine, so i was assuming oh you can initialize. Am i wrong.
/* name3.c -- reads a name using fgets() */
#define MAX 81
#define num 5
/*char *fgets(const char *, int n, FILE *STREAM). Easy enough fgets takes input from the
speccified stream and stores it into the char * pointed to. it reads up to n. its only
drawback is it also stores the newline in the char *. So this uses strcspn to find the index of
'\n' and replace it with the '\0 char to terminate the string.*/
printf("Hi, what's your name?\n");
char *name= fgets(name, MAX, stdin);
/*see this is valid because it is automaitally initialized
to something intersting. interesting. oh the intricacies of C*/
printf("name= %c\n", name[num]);
int ndx = strcspn(name,"\n");
name[ndx] ='\0';/* nice. this is an easy way to use the str char span function.
since it returns an index of the string looking for get the index of the '\n' and replace
it with the null char, thus terminating the string fegets arg will always be a string
anyway of char array, cause you ca't assign to a uninitalized ptr*/
printf("Hey %s. My name is fgets. I normally put a \\n in your name?", name);
/*another example with fgets this time using pointers*/