newbie: char* int and char *int

Discussion in 'C Programming' started by trey, Sep 10, 2003.

  1. trey

    trey Guest

    Hello brilliant people of c.l.c. -

    I have a question about pointer declarations. Is there a difference between
    the following declarations, and if so - what is it ? :)

    char* int;
    char * int;
    char *int;

    The type doesn't have to be char particularly. I'm just wondering if the
    placement of the asterick is seriously important. Seems like it probably
    is, but in this little experimental program I just wrote involving
    pointers, I tried the first and last declarations and there was no
    difference. Of course, this could have been luck and there is something
    undefined going on in the background, which is why I'm posting this ;).
    And since I've seen them used in all three ways, I was curious.

    Thanks for any info...
    -trey
     
    trey, Sep 10, 2003
    #1
    1. Advertising

  2. trey

    Artie Gold Guest

    trey wrote:
    > Hello brilliant people of c.l.c. -
    >
    > I have a question about pointer declarations. Is there a difference between
    > the following declarations, and if so - what is it ? :)
    >
    > char* int;
    > char * int;
    > char *int;
    >
    > The type doesn't have to be char particularly. I'm just wondering if the
    > placement of the asterick is seriously important. Seems like it probably
    > is, but in this little experimental program I just wrote involving
    > pointers, I tried the first and last declarations and there was no
    > difference. Of course, this could have been luck and there is something
    > undefined going on in the background, which is why I'm posting this ;).
    > And since I've seen them used in all three ways, I was curious.
    >



    The placement of the `*' is irrelevant.
    [Of course, none of the above will actually work, as `int' is a
    reserved word.]

    HTH,
    --ag


    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Sep 10, 2003
    #2
    1. Advertising

  3. trey

    Jirka Klaue Guest

    trey wrote:

    > I have a question about pointer declarations. Is there a difference between
    > the following declarations, and if so - what is it ? :)
    >
    > char* int;
    > char * int;
    > char *int;


    The number and positioning of spaces is different. ;-)
    The 3 expressions have the same semantics, though. (If you
    replace int by some allowed identifier name, of course.)

    However, the notation could matter to the human reader.
    Consider:

    char* p, c; /* c is _not_ a char*, despite the notation! */
    char *p, c; /* See? */

    Jirka
     
    Jirka Klaue, Sep 10, 2003
    #3
  4. trey <> wrote:

    >Hello brilliant people of c.l.c. -
    >
    >I have a question about pointer declarations. Is there a difference between
    >the following declarations, and if so - what is it ? :)
    >
    >char* int;

    syntax error

    >char * int;

    sentax yrror

    >char *int;

    sontyx orrer

    See, there's absolutely no difference! :)

    If your question was: what is the difference between

    char* cp;
    char * cp;
    char *cp;

    the answer still is: there is no difference, all are perfectly
    legal in C and have the same meaning, though the last one is
    (mostly) preferred for stylistic reasons.

    Irrwahn

    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 10, 2003
    #4
  5. trey

    trey Guest

    Irrwahn Grausewitz wrote:

    > trey <> wrote:
    >
    >>Hello brilliant people of c.l.c. -
    >>
    >>I have a question about pointer declarations. Is there a difference
    >>between the following declarations, and if so - what is it ? :)
    >>
    >>char* int;

    > syntax error
    >
    >>char * int;

    > sentax yrror
    >
    >>char *int;

    > sontyx orrer
    >
    > See, there's absolutely no difference! :)


    Umm...I have no idea what I was thinking. I mean, I did it in the subject
    line, AND all three declarations. My only morsel of an excuse is that I
    was in the middle of watching Law and Order** with my wife while writing
    it. :)

    >
    > If your question was: what is the difference between
    >
    > char* cp;
    > char * cp;
    > char *cp;
    >
    > the answer still is: there is no difference, all are perfectly
    > legal in C and have the same meaning, though the last one is
    > (mostly) preferred for stylistic reasons.


    Got it. Thanks for the info (to everyone else also). char *cp makes more
    sense to me also, and Jirke Klaue gave a good example where it could be
    confusing not to.


    -trey

    ** A US tv drama about cops and lawyers - don't want to get flamed for
    assuming anything about geography, and it appears you are in fact in
    Germany, at least a .de location using Forte Agent German edition, Standard
    - the name was also a clue, but I wouldn't want to be name-prejudiced. ;)
    Of course I could be wrong about all of it :)
     
    trey, Sep 10, 2003
    #5
  6. Re: [OT] newbie: char* int and char *int

    trey <> wrote:
    <SNIP>
    >
    >[...]I have no idea what I was thinking. I mean, I did it in the subject
    >line, AND all three declarations. My only morsel of an excuse is that I
    >was in the middle of watching Law and Order** with my wife while writing
    >it. :)

    <SNIP>
    >** A US tv drama about cops and lawyers - don't want to get flamed for
    >assuming anything about geography, and it appears you are in fact in
    >Germany, at least a .de location using Forte Agent German edition, Standard
    >- the name was also a clue, but I wouldn't want to be name-prejudiced. ;)
    >Of course I could be wrong about all of it :)


    Nope, you are absolutely right, Mr. Holmes :)

    Irrwahn
    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 10, 2003
    #6
  7. trey

    trey Guest

    Re: [OT] newbie: char* int and char *int

    Irrwahn Grausewitz wrote:

    > trey <> wrote:
    > <SNIP>
    >>
    >>[...]I have no idea what I was thinking. I mean, I did it in the subject
    >>line, AND all three declarations. My only morsel of an excuse is that I
    >>was in the middle of watching Law and Order** with my wife while writing
    >>it. :)

    > <SNIP>
    >>** A US tv drama about cops and lawyers - don't want to get flamed for
    >>assuming anything about geography, and it appears you are in fact in
    >>Germany, at least a .de location using Forte Agent German edition,
    >>Standard - the name was also a clue, but I wouldn't want to be
    >>name-prejudiced. ;) Of course I could be wrong about all of it :)

    >
    > Nope, you are absolutely right, Mr. Holmes :)
    >
    > Irrwahn


    Ha, well, at least I did something right tonight :). (or morning to you I
    believe)

    Cheers
    trey
     
    trey, Sep 10, 2003
    #7
  8. Re: [OT] newbie: char* int and char *int

    trey <> wrote:
    >Irrwahn Grausewitz wrote:
    >> trey <> wrote:
    >> <SNIP>
    >>>** A US tv drama about cops and lawyers - don't want to get flamed for
    >>>assuming anything about geography, and it appears you are in fact in
    >>>Germany, at least a .de location using Forte Agent German edition,
    >>>Standard - the name was also a clue, but I wouldn't want to be
    >>>name-prejudiced. ;) Of course I could be wrong about all of it :)

    >>
    >> Nope, you are absolutely right, Mr. Holmes :)
    >>
    >> Irrwahn

    >
    >Ha, well, at least I did something right tonight :). (or morning to you I
    >believe)
    >

    Yup, 5:20am. Going to bed now, it's been a long night... |-)

    See ya

    Irrwahn
    --
    Computer: a million morons working at the speed of light.
     
    Irrwahn Grausewitz, Sep 10, 2003
    #8
    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. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  4. arun
    Replies:
    8
    Views:
    481
    Dave Thompson
    Jul 31, 2006
  5. gert
    Replies:
    20
    Views:
    1,220
Loading...

Share This Page