c pointers notation basic question

Discussion in 'C Programming' started by bpascal123, Mar 16, 2013.

  1. bpascal123

    bpascal123 Guest

    Hi

    Can someone help me out remember basic pointer notation?

    p points to a type int data
    int *pi;

    char *pc;
    pc points to a type char data

    Is there any difference with this notation?
    char* pc;

    Is pc is still pointing to type char data above?

    I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?

    bpascal
    bpascal123, Mar 16, 2013
    #1
    1. Advertising

  2. In article <>,
    bpascal123 <> wrote:

    > I understand notation '*' operator means pointer. So if it's following the
    > variable type declaration like int* or char*, is it a different meaning as if
    > there is a space like int *var ?


    char *p;
    and
    char* p;
    are equivalent. But see
    http://c-faq.com/decl/charstarws.html
    to see why some prefer one style over the other.
    Mark Storkamp, Mar 16, 2013
    #2
    1. Advertising

  3. bpascal123 wrote:
    >char *pc;
    >pc points to a type char data
    >
    >Is there any difference with this notation?
    >char* pc;


    No.

    "White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
    of the following are equivalent:

    char* pc;
    char *pc;
    char * pc ;
    char *
    pc ;
    char
    *
    pc
    ;

    char


    *


    pc


    ;

    It is recommended to use the form "char *pc;" to make clear what is a
    pointer and what is not when declaring more than one variable in a
    single statement.

    char *yes_a_pointer, not_a_pointer;
    --
    Roberto Waltman

    [ Please reply to the group,
    return address is invalid ]
    Roberto Waltman, Mar 16, 2013
    #3
  4. bpascal123

    bpascal123 Guest

    thanks
    bpascal123, Mar 16, 2013
    #4
  5. bpascal123

    Eric Sosman Guest

    On 3/16/2013 10:24 AM, bpascal123 wrote:
    > Hi
    >
    > Can someone help me out remember basic pointer notation?
    >
    > p points to a type int data
    > int *pi;
    >
    > char *pc;
    > pc points to a type char data
    >
    > Is there any difference with this notation?
    > char* pc;
    >
    > Is pc is still pointing to type char data above?
    >
    > I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?


    No.

    There are only a few places where white space matters in C source:
    When it's the only thing that separates two tokens that would otherwise
    run together (`int main', not `intmain'), when it's part of a string
    literal or character literal ("hello world", ' '), and sometimes in
    preprocessor directives (`#define M(x) y' vs. `#define M (x) y', for
    example). Everywhere else you may add or remove as much white space
    as you please without changing the meaning of the code, provided the
    lines don't get too long. So:

    char *pc;
    char* pc;
    char*pc;
    char/**/*/**/pc/**/;//**//
    char * pc ;

    .... are all equivalent. (Yes, comments are "white space" to the
    compiler, even if they're un-white enough to strain human eyes!)

    Most C programmers I've met seem to prefer

    char *pc;

    as more readable than the others. It's thought that this form
    may reduce the likelihood of making errors like

    char* firstName, lastName; // two strings

    .... which does not do what the comment suggests (see Question 1.5
    on the comp.lang.c Frequently Asked Questions -- FAQ -- page at
    <http://www.c-faq.com/index.html>).

    --
    Eric Sosman
    d
    Eric Sosman, Mar 16, 2013
    #5
  6. bpascal123

    Shao Miller Guest

    On 3/16/2013 10:24, bpascal123 wrote:
    > Hi
    >
    > Can someone help me out remember basic pointer notation?
    >
    > p points to a type int data
    > int *pi;
    >
    > char *pc;
    > pc points to a type char data
    >
    > Is there any difference with this notation?
    > char* pc;
    >
    > Is pc is still pointing to type char data above?
    >
    > I understand notation '*' operator means pointer. So if it's following the variable type declaration like int* or char*, is it a different meaning as if there is a space like int *var ?
    >


    The following are the same:

    char*pc;
    char * pc;
    char* pc;
    char *pc;
    char(*pc);
    char(*(pc));
    char (* pc);

    et cetera.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 16, 2013
    #6
  7. In article <-september.org>,
    Mark Storkamp <> wrote:
    >
    >But see
    >http://c-faq.com/decl/charstarws.html



    I read that as "Char Star Wars". Anyway, I totally agree 100% with
    the point made on that web page. That's how I do it. That's how all
    right-thinking people do it. People who do it the other way probably
    kick puppies too.

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
    Edward A. Falk, Mar 16, 2013
    #7
  8. bpascal123

    Shao Miller Guest

    On 3/16/2013 13:55, Edward A. Falk wrote:
    > In article <-september.org>,
    > Mark Storkamp <> wrote:
    >>
    >> But see
    >> http://c-faq.com/decl/charstarws.html

    >
    >
    > I read that as "Char Star Wars". Anyway, I totally agree 100% with
    > the point made on that web page. That's how I do it. That's how all
    > right-thinking people do it. People who do it the other way probably
    > kick puppies too.
    >


    That's just what a right-handed person might say. Obviously,

    type * p, * q;

    is far superior to:

    type* p, * q;

    and

    type *p, *q;

    ;)

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 16, 2013
    #8
  9. bpascal123

    Ian Collins Guest

    Edward A. Falk wrote:
    > In article <-september.org>,
    > Mark Storkamp <> wrote:
    >>
    >> But see
    >> http://c-faq.com/decl/charstarws.html

    >
    >
    > I read that as "Char Star Wars". Anyway, I totally agree 100% with
    > the point made on that web page. That's how I do it. That's how all
    > right-thinking people do it. People who do it the other way probably
    > kick puppies too.


    The argument presented is moot if your style rules (are any good and..)
    prevent multiple variable declarations one one line.

    :)

    --
    Ian Collins
    Ian Collins, Mar 16, 2013
    #9
  10. Roberto Waltman <> writes:

    > bpascal123 wrote:
    >>char *pc;
    >>pc points to a type char data
    >>
    >>Is there any difference with this notation?
    >>char* pc;

    >
    > No.
    >
    > "White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
    > of the following are equivalent:

    [snip]
    > It is recommended to use the form "char *pc;" to make clear what is a
    > pointer and what is not when declaring more than one variable in a
    > single statement.
    >
    > char *yes_a_pointer, not_a_pointer;


    It's also recommended not to declare more than one variable in a single
    declaration (not statement):

    char *yes_a_pointer;
    char not_a_pointer;

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 16, 2013
    #10
  11. Eric Sosman <> writes:
    [...]
    > Most C programmers I've met seem to prefer
    >
    > char *pc;
    >
    > as more readable than the others. It's thought that this form
    > may reduce the likelihood of making errors like
    >
    > char* firstName, lastName; // two strings
    >
    > ... which does not do what the comment suggests (see Question 1.5
    > on the comp.lang.c Frequently Asked Questions -- FAQ -- page at
    > <http://www.c-faq.com/index.html>).


    I can't resist pointing out that this:

    char *firstName, *lastName; // two strings

    also doesn't do what the comment suggests. firstName and lastName
    may be *pointers* to strings if you assign appropriate values
    to them.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 16, 2013
    #11
  12. On 16-Mar-13 17:55, Keith Thompson wrote:
    > Roberto Waltman <> writes:
    >> It is recommended to use the form "char *pc;" to make clear what is
    >> a pointer and what is not when declaring more than one variable in
    >> a single statement.
    >>
    >> char *yes_a_pointer, not_a_pointer;

    >
    > It's also recommended not to declare more than one variable in a
    > single declaration (not statement):
    >
    > char *yes_a_pointer;
    > char not_a_pointer;


    "char *pc" is clearly superior to "char* pc"; I'm not aware of any case
    where that would impair readability, nor anyone who would seriously
    recommend the latter.

    I also agree that the form "char* pc, c;" is to be avoided, simply
    because it's visually similar to a common and obvious mistake.

    However, there are cases where declaring one variable per line hurts
    more than it helps. It is more common, in my experience, to see one
    type per line:

    char *a, *b, *c, *d;
    char e, f, g, h;
    int i, j, k, l;

    If the identifiers are meaningful and relatively short, at least. If
    they're long, or if they're cryptic enough to merit comments, then one
    per line starts to make sense.

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
    Stephen Sprunk, Mar 16, 2013
    #12
  13. Ian Collins <> writes:
    > Edward A. Falk wrote:
    >> In article <-september.org>,
    >> Mark Storkamp <> wrote:
    >>>
    >>> But see
    >>> http://c-faq.com/decl/charstarws.html

    >>
    >> I read that as "Char Star Wars". Anyway, I totally agree 100% with
    >> the point made on that web page. That's how I do it. That's how all
    >> right-thinking people do it. People who do it the other way probably
    >> kick puppies too.

    >
    > The argument presented is moot if your style rules (are any good and..)
    > prevent multiple variable declarations one one line.
    >
    > :)


    Agreed. On the other hand, I still prefer
    char *p;
    over
    char* p;
    because it reflects the grammar.

    On the other other hand, if I always followed that rule, my if /
    else / else if statements would crawl across the screen.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 16, 2013
    #13
  14. bpascal123

    Ian Collins Guest

    Stephen Sprunk wrote:
    > On 16-Mar-13 17:55, Keith Thompson wrote:
    >> Roberto Waltman <> writes:
    >>> It is recommended to use the form "char *pc;" to make clear what is
    >>> a pointer and what is not when declaring more than one variable in
    >>> a single statement.
    >>>
    >>> char *yes_a_pointer, not_a_pointer;

    >>
    >> It's also recommended not to declare more than one variable in a
    >> single declaration (not statement):
    >>
    >> char *yes_a_pointer;
    >> char not_a_pointer;

    >
    > "char *pc" is clearly superior to "char* pc"; I'm not aware of any case
    > where that would impair readability, nor anyone who would seriously
    > recommend the latter.
    >
    > I also agree that the form "char* pc, c;" is to be avoided, simply
    > because it's visually similar to a common and obvious mistake.
    >
    > However, there are cases where declaring one variable per line hurts
    > more than it helps. It is more common, in my experience, to see one
    > type per line:
    >
    > char *a, *b, *c, *d;
    > char e, f, g, h;
    > int i, j, k, l;
    >
    > If the identifiers are meaningful and relatively short, at least. If
    > they're long, or if they're cryptic enough to merit comments, then one
    > per line starts to make sense.


    Of if you prefer to declare and initialise them at the point of first use.

    --
    Ian Collins
    Ian Collins, Mar 16, 2013
    #14
  15. bpascal123 <> wrote:

    > Can someone help me out remember basic pointer notation?


    (snip)
    > char *pc;
    > pc points to a type char data


    > Is there any difference with this notation?
    > char* pc;


    > Is pc is still pointing to type char data above?


    Those are the same, but one is less readable.

    It is more interesting in Java, where there is:

    char []pc;

    and

    char pc[];

    Which again mean the same thing, and similar to the
    C meaning of char *pc, but:

    char[] x,y;
    char x[], y;
    char x,y[];

    Mean three different things.

    -- glen
    glen herrmannsfeldt, Mar 16, 2013
    #15
  16. Stephen Sprunk <> writes:
    > On 16-Mar-13 17:55, Keith Thompson wrote:
    >> Roberto Waltman <> writes:
    >>> It is recommended to use the form "char *pc;" to make clear what is
    >>> a pointer and what is not when declaring more than one variable in
    >>> a single statement.
    >>>
    >>> char *yes_a_pointer, not_a_pointer;

    >>
    >> It's also recommended not to declare more than one variable in a
    >> single declaration (not statement):
    >>
    >> char *yes_a_pointer;
    >> char not_a_pointer;

    >
    > "char *pc" is clearly superior to "char* pc"; I'm not aware of any case
    > where that would impair readability, nor anyone who would seriously
    > recommend the latter.


    Bjarne Stroustrup, as I recall, advocates "char* pc;" (and avoiding
    multiple declarations per line), and many, perhaps most, C++ programmers
    follow that convention. It's easily read as "pc is of type char*", even
    though it *really* means "*pc is of type char, from which you can infer
    that pc is of type char*".

    Yes, C and C++ are distinct languages, but they share the same syntax
    and semantics in this particular area.

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 16, 2013
    #16
  17. bpascal123

    Eric Sosman Guest

    On 3/16/2013 6:55 PM, Keith Thompson wrote:
    > Roberto Waltman <> writes:
    >
    >> bpascal123 wrote:
    >>> char *pc;
    >>> pc points to a type char data
    >>>
    >>> Is there any difference with this notation?
    >>> char* pc;

    >>
    >> No.
    >>
    >> "White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
    >> of the following are equivalent:

    > [snip]
    >> It is recommended to use the form "char *pc;" to make clear what is a
    >> pointer and what is not when declaring more than one variable in a
    >> single statement.
    >>
    >> char *yes_a_pointer, not_a_pointer;

    >
    > It's also recommended not to declare more than one variable in a single
    > declaration (not statement):
    >
    > char *yes_a_pointer;
    > char not_a_pointer;


    Yah. Shrug. Whaddevah.

    double sumx;
    double sumy;
    double sumxx;
    double sumyy;
    double sumxy;

    *So* much better than

    double sumx, sumy, sumxx, sumyy, sumxy;

    It's a crying shame the grammar permits the latter, don't you think?

    --
    Eric Sosman
    d
    Eric Sosman, Mar 17, 2013
    #17
  18. bpascal123

    James Kuyper Guest

    On 03/16/2013 04:56 PM, Ian Collins wrote:
    ....
    > The argument presented is moot if your style rules (are any good and..)
    > prevent multiple variable declarations one one line.


    prevent => prohibit?
    --
    James Kuyper
    James Kuyper, Mar 17, 2013
    #18
  19. bpascal123

    Öö Tiib Guest

    On Sunday, 17 March 2013 02:34:01 UTC+2, Eric Sosman wrote:
    > On 3/16/2013 6:55 PM, Keith Thompson wrote:
    > > Roberto Waltman <> writes:
    > >> bpascal123 wrote:
    > >>> char *pc;
    > >>> pc points to a type char data
    > >>>
    > >>> Is there any difference with this notation?
    > >>> char* pc;
    > >>
    > >> No.
    > >>
    > >> "White space" (spaces, tabs, new-lines, etc.) is not relevant, so all
    > >> of the following are equivalent:

    > > [snip]
    > >> It is recommended to use the form "char *pc;" to make clear what is a
    > >> pointer and what is not when declaring more than one variable in a
    > >> single statement.
    > >>
    > >> char *yes_a_pointer, not_a_pointer;

    > >
    > > It's also recommended not to declare more than one variable in a single
    > > declaration (not statement):
    > >
    > > char *yes_a_pointer;
    > > char not_a_pointer;

    >
    > Yah. Shrug. Whaddevah.
    >
    > double sumx;
    > double sumy;
    > double sumxx;
    > double sumyy;
    > double sumxy;
    >
    > *So* much better than
    >
    > double sumx, sumy, sumxx, sumyy, sumxy;
    >
    > It's a crying shame the grammar permits the latter, don't you think?


    It is also often recommended to declare variables close to first usage
    and if possible then to initialize at point of declaration.

    It may well be that both above examples are doing neither. Can you bring
    bit longer example where the declarations were closest to first usage and
    where it does not make sense to initialize them at point of declaration?
    Öö Tiib, Mar 17, 2013
    #19
  20. bpascal123

    Shao Miller Guest

    On 3/16/2013 21:55, Öö Tiib wrote:
    >
    > It is also often recommended to declare variables close to first usage
    > and if possible then to initialize at point of declaration.
    >


    It might be often, but not often when considering C89/C90 compatibility,
    of course.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 17, 2013
    #20
    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. Simon

    Basic pointers question

    Simon, Nov 17, 2006, in forum: C Programming
    Replies:
    6
    Views:
    325
  2. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,300
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  3. Tameem
    Replies:
    454
    Views:
    11,863
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    673
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    467
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page