char *fred; char * fred; char *fred; any difference?

Discussion in 'C Programming' started by Ben Pfaff, Jan 16, 2004.

  1. Ben Pfaff

    Ben Pfaff Guest

    "Hal Styli" <no_spam@all> writes:

    > can anyone explain the difference between the above declarations and why
    > anyone would stray from the first.
    > Are the other 2 standard, couldnt find them in K&R2.


    I suppose you're referring to the subject of your article, which
    is
    char *fred; char * fred; char *fred; any difference?
    These all have the same meaning, as does char*fred.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, Jan 16, 2004
    #1
    1. Advertising

  2. Ben Pfaff

    Hal Styli Guest

    Hello,

    can anyone explain the difference between the above declarations and why
    anyone would stray from the first.
    Are the other 2 standard, couldnt find them in K&R2.

    Thanks,
    Hal.
    Hal Styli, Jan 16, 2004
    #2
    1. Advertising

  3. On Fri, 16 Jan 2004 22:10:54 -0000, in comp.lang.c , "Hal Styli"
    <no_spam@all> wrote:

    >Hello,
    >
    >can anyone explain the difference between the above declarations


    what "above" declarations??? There's no declarations in your post.

    (In other words, don't put your question in the subject, put it in the
    mail body. Not all mail readers display teh subject next to the body,

    Anyway all three are identical. As is
    char * fred;
    because whitespace is totally immaterial in (most) C statements.

    >and why anyone would stray from the first.


    C++ programmers prefer to attach the * to the type. C programmers to
    the object. Either is acceptable.

    >Are the other 2 standard, couldnt find them in K&R2.


    K&R is a C book, and the authors used their preferred style.



    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Mark McIntyre, Jan 16, 2004
    #3
  4. Mark McIntyre wrote:
    [...]
    > Anyway all three are identical. As is
    > char * fred;
    > because whitespace is totally immaterial in (most) C statements.
    >
    > >and why anyone would stray from the first.

    >
    > C++ programmers prefer to attach the * to the type. C programmers to
    > the object. Either is acceptable.


    In the case of a single variable, yes. Of course:

    char *ptr,c;

    is probably clearer than:

    char* ptr,c;

    But, the above are still identical in meaning, and it's mostly a
    matter of personal preference. (Sort of like "where do I put the
    braces for for/while/etc. statements?")

    [...]

    --

    +---------+----------------------------------+-----------------------------+
    | Kenneth | kenbrody at spamcop.net | "The opinions expressed |
    | J. | http://www.hvcomputer.com | herein are not necessarily |
    | Brody | http://www.fptech.com | those of fP Technologies." |
    +---------+----------------------------------+-----------------------------+
    Kenneth Brody, Jan 16, 2004
    #4
  5. Ben Pfaff

    CBFalconer Guest

    Kenneth Brody wrote:
    > Mark McIntyre wrote:
    > [...]
    > >
    > > C++ programmers prefer to attach the * to the type.
    > > C programmers to the object. Either is acceptable.

    >
    > In the case of a single variable, yes. Of course:
    >
    > char *ptr,c;
    >
    > is probably clearer than:
    >
    > char* ptr,c;
    >
    > But, the above are still identical in meaning, and it's mostly
    > a matter of personal preference. (Sort of like "where do I put
    > the braces for for/while/etc. statements?")


    C programmers prefer "char *ptr" because, while " char *"
    describes the type pointer to char, it can't be used for multiple
    items due to Cs weird and wonderful declaration syntax. However
    you can use (but often frowned upon as obfuscative):

    typedef char *charptr;
    charptr cp1, cp2, cp3;

    which has a much different meaning for cp2 and cp3 than:

    char * cp1, cp2, cp3;

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jan 17, 2004
    #5
  6. Greetings.

    In article <4008617d$1_5@127.0.0.1>, Hal Styli wrote:
    > can anyone explain the difference between the above declarations and why
    > anyone would stray from the first.
    > Are the other 2 standard, couldnt find them in K&R2.


    Many people prefer keeping the * next to the variable rather than the type,
    as this avoids the implication that the * is distributive. However,
    syntactically the position doesn't matter.

    Regards,
    Tristan

    --
    _
    _V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
    / |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
    (7_\\ http://www.nothingisreal.com/ >< To finish what you
    Tristan Miller, Jan 17, 2004
    #6
    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. jakk
    Replies:
    4
    Views:
    12,118
  2. wwj
    Replies:
    7
    Views:
    548
  3. wwj
    Replies:
    24
    Views:
    2,503
    Mike Wahler
    Nov 7, 2003
  4. Nick Timkovich
    Replies:
    0
    Views:
    67
    Nick Timkovich
    Feb 25, 2014
  5. Skip Montanaro
    Replies:
    0
    Views:
    63
    Skip Montanaro
    Feb 25, 2014
Loading...

Share This Page