Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: What does the asteriks mean?

Thread Tools

Re: What does the asteriks mean?

Tom St Denis
Posts: n/a
      06-25-2003 Removed) 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.


Reply With Quote
Posts: n/a
Hey Tom,

Thanks for the in depth explanation. I appreciate you taking the time
to do that!
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
The modem for Asteriks Antonio VOIP 4 05-20-2004 05:46 PM
Asteriks! What i need? Antonio VOIP 2 05-14-2004 06:47 AM
Re: What does the asteriks mean? C Programming 1 07-08-2003 07:51 PM
Re: What does the asteriks mean? C Programming 5 06-26-2003 10:13 PM
Re: What does the asteriks mean? Dan Pop C Programming 0 06-26-2003 12:57 PM