Results 1 to 5 of 5

Thread: Coding style about pointer

  1. #1
    Join Date
    Jun 2006
    Location
    Vietnam
    Posts
    59
    Thanks
    17
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Coding style about pointer

    I read a lot ebooks about C++. There are 2 way to declare a pointer, I don't know we should use which way although they are similar? And give me a reason.
    Qt Code:
    1. char* str;
    2. or
    3. char *str;
    To copy to clipboard, switch view to plain text mode 
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding style about pointer

    There's also a third way:
    Qt Code:
    1. char * str;
    To copy to clipboard, switch view to plain text mode 
    An it really doesn't matter which one you choose, as long as you use it consistently.

  3. The following user says thank you to jacek for this useful post:

    vql (4th February 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding style about pointer

    I second jacek on this. This is a very small thing and consistency is what matters. I just want to point out that Qt used the second notation that is char *str;

    Rationale:
    Qt Code:
    1. char* str1, str2; // here str1 is a char* and str2 is a char;
    2.  
    3. char *str1, str2; // Here the declaration is more clear since the * is attached with the variable
    To copy to clipboard, switch view to plain text mode 

    and FYI Qt uses the char *str; notation
    We can't solve problems by using the same kind of thinking we used when we created them

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Coding style about pointer

    It really doesn't matter The parser regognizes non-alphanumeric characters as token boundaries, so you may put as much spaces or other characters there. These all are valid as well:
    Qt Code:
    1. char
    2. *
    3. str1,
    4. str2
    5. ;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. char *str1
    2. ,str2;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. x=5;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. x = 5;
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. x
    2. =5;
    To copy to clipboard, switch view to plain text mode 

    It is all just about source code readability.

  6. #5
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Coding style about pointer

    Quote Originally Posted by wysota
    It is all just about source code readability.
    Exactly !!

    The point is that char *str1, str2 seems to be more clear ( to me ). That is it. And I found this practice in Qt Sources.
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. about scrollbar style
    By qtopiahooo in forum Qt Programming
    Replies: 1
    Last Post: 25th January 2007, 14:34
  2. Custom Style
    By Dusdan in forum Qt Tools
    Replies: 8
    Last Post: 1st September 2006, 23:50
  3. simple pointer question
    By mickey in forum General Programming
    Replies: 6
    Last Post: 16th June 2006, 10:19
  4. What is your coding style ??
    By guestgulkan in forum General Discussion
    Replies: 9
    Last Post: 29th May 2006, 10:22
  5. Bugs, style changes in 4.1.0?
    By simk in forum Qt Programming
    Replies: 13
    Last Post: 13th February 2006, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.