Thread: Difference

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    Difference

    What is the difference between the following?

    char* mystring;
    char *mystring;

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nothing but semantics - ie how it looks.
    http://www.research.att.com/~bs/bs_faq2.html#whitespace
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I also assume there's no difference between char** mystring and char **mystring either right?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    what about char* *mystring?

    :P

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Nope, same thing. To the compiler, it doesn't matter.
    I tend to emphasize the type, so I write char***--etc.
    You could also write char** * to emphasize that it is a pointer (the lone *) to char** (the type to the far left).
    In the end, it's up to you.

    I emphasize types because pointers are NOT the same as non-pointers, so assigning int* to int will cause complaints.
    Thus I see that the pointer is of type char* and points to char (remove the *).
    Also, many tend to see the * as part of the name, but in reality it is not, because "*" is not a valid character in a name, which is another reason I put it on the left.
    Last edited by Elysia; 02-13-2009 at 10:29 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    As always, Thanks!

  7. #7
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    For a different view, here's my opinion:

    Always put the "*" next to the name in a variable definition, i.e. use "char *foo" instead of "char* foo".

    Why?

    Consider the following code:

    Code:
    char* foo, bar;
    To me, this looks like foo and bar are both pointers to char, when in fact foo is a pointer to char and bar is not.


    On the other hand, the "*" modifies the type, so it may also belong to the type name, not the variable name. It's a matter of style and, most importantly, consistency.

    Greets,
    Philip

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I see the what you mean Philip. Personally I always put one variable declaration per line.

    char *foo;
    char *bar;

    mostly so I can put comments after each line so I know what the variables are for.

    That being said I still always choose to use char *foo rather than char* bar.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is one of the issues Bjarne picks up. It's best to always put each pointer definition on a separate row to avoid confusion.
    And that's a good advice, regardless of style.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Using pascal convention like multiline variable's type definition might be a little help to the code cleanliness.
    Code:
    typedef int TInt;
    typedef TInt *PInt;
    
    struct ArrayOfInt {
    PInt array;
    TInt length;
    };
    
    void add(PInt buffer){ }
    Just GET it OFF out my mind!!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    However, typedefs for pointers, ESPECIALLY with misleading names, is a very bad idea™.
    Code:
    struct ArrayOfInt
    {
        int array;
        int* length;
    };
    
    void add(int* buffer) { }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM