Re: What does the asteriks mean?

Discussion in 'C Programming' started by Tom St Denis, Jun 26, 2003.

  1. Tom St Denis

    Tom St Denis Guest

    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
    Tom St Denis, Jun 26, 2003
    #1
    1. Advertising

  2. Tom St Denis

    Guest

    Hey Tom,

    Thanks for the in depth explanation. I appreciate you taking the time
    to do that!
    , Jun 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Re: What does the asteriks mean?

    , Jun 26, 2003, in forum: C Programming
    Replies:
    5
    Views:
    1,051
    Mark McIntyre
    Jun 26, 2003
  2. Dan Pop

    Re: What does the asteriks mean?

    Dan Pop, Jun 26, 2003, in forum: C Programming
    Replies:
    0
    Views:
    655
    Dan Pop
    Jun 26, 2003
  3. Re: What does the asteriks mean?

    , Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    574
    Keith Thompson
    Jul 8, 2003
  4. Li Ma
    Replies:
    1
    Views:
    2,258
    Roedy Green
    Mar 9, 2009
  5. C Barrington-Leigh
    Replies:
    1
    Views:
    1,172
    Tim Leslie
    Sep 10, 2010
Loading...

Share This Page