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
  1. Home
  2. The Lounge
  3. Why does most C/C++ developer prefers char *c instead of char* c?

Why does most C/C++ developer prefers char *c instead of char* c?

Scheduled Pinned Locked Moved The Lounge
questionc++
58 Posts 33 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jpg 0
    wrote on last edited by
    #1

    char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

    L M R J P 27 Replies Last reply
    0
    • J jpg 0

      char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No idea from where this comes, but I also prefer char* c before char *c. My only Argument: In case you have a e.g. a method with an argument you don't use, something like void Method(Object* Sender) my Compiler shows a warning. To avoid the warning I Need to void Method(Object* /*Sender*/) or void Method(Object* ) that's why for me char* c is more intuitive. On the other Hand variable declaration.... char *a, *b;

      It does not solve my Problem, but it answers my question

      1 Reply Last reply
      0
      • J jpg 0

        char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

        M Offline
        M Offline
        Mike Hankey
        wrote on last edited by
        #3

        I also prefer char* c as char* is a type, in this case a pointer to a char.

        Everyone has a photographic memory; some just don't have film. Steven Wright

        1 Reply Last reply
        0
        • J jpg 0

          char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Goes back to K&R, they invented the thing and people followed their style. also in C you cant declare arrays that way:

          char c[]; /* compiles just fine */
          char[] d; /* error */
          // so followig the same
          Tis the style of this language, inasmuch to continue that style far more logical to write */
          char *e;

          1. and so "char* c" is stylistically wrong in both C & C++. (And older compilers will correctly flag that as an error.) 2. In C "char *" is not a type, it's a pointer to a type. Yup, pointers are not types, they are pointers. ... cats, dogs and cows are types of animal, beef is not an animal but it does 'refer' to one

          Signature ready for installation. Please Reboot now.

          L 2 Replies Last reply
          0
          • L Lost User

            Goes back to K&R, they invented the thing and people followed their style. also in C you cant declare arrays that way:

            char c[]; /* compiles just fine */
            char[] d; /* error */
            // so followig the same
            Tis the style of this language, inasmuch to continue that style far more logical to write */
            char *e;

            1. and so "char* c" is stylistically wrong in both C & C++. (And older compilers will correctly flag that as an error.) 2. In C "char *" is not a type, it's a pointer to a type. Yup, pointers are not types, they are pointers. ... cats, dogs and cows are types of animal, beef is not an animal but it does 'refer' to one

            Signature ready for installation. Please Reboot now.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I would say "*" is a type modifier :-D [Edit]

            Quote:

            In C "char *" is not a type

            Really? Ok I don't know C, but I think this is also allowed in C typedef char@ theCharPointerType; @ instead of *, because * seems to be a Problem in cp And if the above is ok in C, how can you state that a pointer to char is not a type?

            It does not solve my Problem, but it answers my question

            L 2 Replies Last reply
            0
            • J jpg 0

              char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

              R Offline
              R Offline
              Ron Anders
              wrote on last edited by
              #6

              char *c was the old K&R way. Yes char* c looks better.

              1 Reply Last reply
              0
              • L Lost User

                I would say "*" is a type modifier :-D [Edit]

                Quote:

                In C "char *" is not a type

                Really? Ok I don't know C, but I think this is also allowed in C typedef char@ theCharPointerType; @ instead of *, because * seems to be a Problem in cp And if the above is ok in C, how can you state that a pointer to char is not a type?

                It does not solve my Problem, but it answers my question

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                cant be a type modifier either, char *c --> * means pointer, "char" is is the default 'pointed to' modifier for that declaration. In the language definition it's valid to cast a * to address any [other] type. If the type was considered "char*" or in fact if "char" had anything to do with 'the type' then casting pointers to address other type would become wrong. hence:

                char* c" /* is wrong, "char*" is NOT the type because there is no such thing, it's wrong, plain wrong! */
                char *c /* is the proper form */

                Signature ready for installation. Please Reboot now.

                L 1 Reply Last reply
                0
                • L Lost User

                  cant be a type modifier either, char *c --> * means pointer, "char" is is the default 'pointed to' modifier for that declaration. In the language definition it's valid to cast a * to address any [other] type. If the type was considered "char*" or in fact if "char" had anything to do with 'the type' then casting pointers to address other type would become wrong. hence:

                  char* c" /* is wrong, "char*" is NOT the type because there is no such thing, it's wrong, plain wrong! */
                  char *c /* is the proper form */

                  Signature ready for installation. Please Reboot now.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Can be, it is anyway a more philosophical Thing. In case char* is plain wrong it should not be allowed to do this: typedef char* myCharPtr; :-D

                  It does not solve my Problem, but it answers my question

                  1 Reply Last reply
                  0
                  • L Lost User

                    I would say "*" is a type modifier :-D [Edit]

                    Quote:

                    In C "char *" is not a type

                    Really? Ok I don't know C, but I think this is also allowed in C typedef char@ theCharPointerType; @ instead of *, because * seems to be a Problem in cp And if the above is ok in C, how can you state that a pointer to char is not a type?

                    It does not solve my Problem, but it answers my question

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Lopatir is correct. The way to read the declaration

                    char *c

                    is: c (the name of the variable) is a pointer (the * prefix) to a character (the type) where the pointer property actually belongs to the variable, not the type. Having said that I always write:

                    char* c

                    ;)

                    L 1 Reply Last reply
                    0
                    • L Lost User

                      Lopatir is correct. The way to read the declaration

                      char *c

                      is: c (the name of the variable) is a pointer (the * prefix) to a character (the type) where the pointer property actually belongs to the variable, not the type. Having said that I always write:

                      char* c

                      ;)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      How the hell you can break such a rule? Please refracte (does this word really exists?) all your source code ...please!!! :laugh: [Edit] Btw, I don't think Lopidar is right. Started with Modula and read a lot from "Niklaus Wirth" theories, also I did a lot of Compiler implementations ;)

                      It does not solve my Problem, but it answers my question

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        How the hell you can break such a rule? Please refracte (does this word really exists?) all your source code ...please!!! :laugh: [Edit] Btw, I don't think Lopidar is right. Started with Modula and read a lot from "Niklaus Wirth" theories, also I did a lot of Compiler implementations ;)

                        It does not solve my Problem, but it answers my question

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        0x01AA wrote:

                        Please refracte

                        The correct word would be "refactor". But at my age I may not have enough time to get it all done. :((

                        L 1 Reply Last reply
                        0
                        • L Lost User

                          0x01AA wrote:

                          Please refracte

                          The correct word would be "refactor". But at my age I may not have enough time to get it all done. :((

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          So I Need to write: Do refactor your code?

                          Quote:

                          But at my age I may not have enough time to get it all done

                          Stop thinking like this!! You have all the time

                          It does not solve my Problem, but it answers my question

                          1 Reply Last reply
                          0
                          • J jpg 0

                            char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

                            J Offline
                            J Offline
                            jschell
                            wrote on last edited by
                            #13

                            I prefer "char* c". Long ago I used the other form but an article I read long ago convinced me that the 'type' should be emphasized as different from the variable.

                            L 1 Reply Last reply
                            0
                            • J jpg 0

                              char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              To remind users that char* a , b may not do what they intend.

                              C D 2 Replies Last reply
                              0
                              • J jpg 0

                                char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

                                C Offline
                                C Offline
                                Chris Maunder
                                wrote on last edited by
                                #15

                                the variable is the type, and the type stays the type. The * goes with the variable because you're modifying defining how the variable will be using the type. You're not, as it were, modifying the type.

                                cheers Chris Maunder

                                L U P 3 Replies Last reply
                                0
                                • J jschell

                                  I prefer "char* c". Long ago I used the other form but an article I read long ago convinced me that the 'type' should be emphasized as different from the variable.

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  how do you do: "char c[]" ?? "char[] c" wont compile, so that "type/name" logic is already broken for C/C++. The article you read was written by someone that either referred to a different programming language, or doesn't understand the C/C++ language definitions; char* is not a type in C/C++. For real fun, have you considered "char *c[]" ... writing that the wrong way as "char* c[]" obviously looks, reads and is just plain wrong because that would read as an "array of pointers" when what I wanted was a "pointer to an array." Personal style is OK, but justifying it as proper with a mistake isn't. In short: if you prefer the look of "char* c" carry on, just remember it's a pointer, not a type.

                                  Signature ready for installation. Please Reboot now.

                                  J 1 Reply Last reply
                                  0
                                  • C Chris Maunder

                                    the variable is the type, and the type stays the type. The * goes with the variable because you're modifying defining how the variable will be using the type. You're not, as it were, modifying the type.

                                    cheers Chris Maunder

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #17

                                    Chris Maunder wrote:

                                    the variable is the type, and the type stays the type. The * goes with the variable because you're modifying defining how the variable will be using accessing the type value. You're not, as it were, modifying the type.

                                    Signature ready for installation. Please Reboot now.

                                    C U 2 Replies Last reply
                                    0
                                    • J jpg 0

                                      char is a type and c is a name, to me, it always make more sense to put the name alone and have the type together, like "char* c", I can tell immediately that it is a pointer to a char, so its always goes like [type] [name]. But in contrast, most C/C++ code I found prefer the other way around, like "char *c". Is there any specific reasons why this is so?

                                      J Offline
                                      J Offline
                                      Jon McKee
                                      wrote on last edited by
                                      #18

                                      I've always liked option 3: char * c. It avoids the problems char* c, d; can cause but still keeps it separate from the name. * is just like const or any other modifier. You wouldn't write constchar* c so why mash them together just because it's a single character (and allowed)?

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        Chris Maunder wrote:

                                        the variable is the type, and the type stays the type. The * goes with the variable because you're modifying defining how the variable will be using accessing the type value. You're not, as it were, modifying the type.

                                        Signature ready for installation. Please Reboot now.

                                        C Offline
                                        C Offline
                                        Chris Maunder
                                        wrote on last edited by
                                        #19

                                        OK, fair enough.

                                        cheers Chris Maunder

                                        1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          To remind users that char* a , b may not do what they intend.

                                          C Offline
                                          C Offline
                                          CPallini
                                          wrote on last edited by
                                          #20

                                          Exactly.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

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