Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
H

Hans Salvisberg

@Hans Salvisberg
About
Posts
2
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Why does most C/C++ developer prefers char *c instead of char* c?
    H Hans Salvisberg

    nullusDefectus wrote:

    Also, the declaration char *c, d makes c a char*, but d a char. This confuses beginners who are used to declarations such as int a, b which makes both a and b ints. Thus, the declaration char* c is preferred: easier to learn and safer.

    Your claim and conclusion are backwards! It's writing char* c, d; that confuses beginners for the exact reason that you give. Writing char *c, d; is the correct way to teach, learn, and remind yourself and readers of your code that the * applies to c only. Given the language grammar...

    simple-declaration ::= decl-specifier-seq init-declarator-list(optional) ;
    Note the space ---^

    ... writing...

    char *c;
    ---^

    ... also demonstrates that you know what you're doing. This is how you explain it to beginners and they will remember... ;)

    The Lounge question c++

  • Why does most C/C++ developer prefers char *c instead of char* c?
    H Hans Salvisberg

    As others have pointed out before, the issue is char* c, d; vs. char *c, *d; The C++ grammar says that a declaration has the form decl-specifier-seq init-declarator-list; char is the decl-specifier, and what follows is one or more declarators (with optional initializers). *c is a declarator, and *d (or plain d) is another one. So, if you write char* c then you're needlessly confusing your (and your reader's) mental image of the C++ grammar.

    The Lounge question c++
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups