Originally Posted by
Prelude
>char *name2=fgets(name2,MAX,stdin);
*Huge hint* What does name2 point to when you pass it to fgets?
See that's what I thought, its uninitialized, but isn't it initialized with the return value of fgets?
see i had this code before
PHP Code:
/* name3.c -- reads a name using fgets() */
#include <stdio.h>
#define MAX 81
#define num 5
int main(void)
{
/*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.*/
//char name[MAX];
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[4]= %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);
getchar();
return 0;
/*another example with fgets this time using pointers*/
}
and it works fine, so i was assuming oh you can initialize. Am i wrong.