wrote:
> What does the * mean before the p in the following code? I figure
> that it has something to do with pointers, but I haven't learned about
> pointers yet.
* w.r.t. pointers means to dereference a pointer. Sounds complicated
but isn't.
char *p;
p is a pointer to a char. now
p = "a"
means assign the address of the literal "a" to p.
*p = 'a'
means to find out where in memory p points to and put the char 'a' there.
> ========
>
> #include <stdio.h>
> #include <string.h>
>
> char answer[100], *p;
> printf("Type something:\n");
> fgets(answer, sizeof answer, stdin);
> if((p = strchr(answer, '\n')) != NULL)
> *p = '\0';
> printf("You typed \"%s\"\n", answer);
In this example, strchr returns the address of the first occurence of
the letter '\n' [the C newline].
*p = '\0'
means to assign a null byte [value zero] to that location. In C all
strings are terminated with a null byte. So in effect this will replace
the first \n with a null.
Tom