Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: What does the asteriks mean?

 
 
Tom St Denis
Guest
Posts: n/a
 
      06-25-2003
http://www.velocityreviews.com/forums/(E-Mail 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.

Tom

 
Reply With Quote
 
 
 
 
FakeAddress@NoSpam.com
Guest
Posts: n/a
 
      06-26-2003
Hey Tom,

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

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? sumit.sharma@wipro.com C Programming 1 07-08-2003 07:51 PM
Re: What does the asteriks mean? FakeAddress@NoSpam.com 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



Advertisments