Why does most C/C++ developer prefers char *c instead of char* c?
-
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. :((
-
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?
-
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?
To remind users that
char* a , b
may not do what they intend. -
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?
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
-
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.
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.
-
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
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.
-
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?
I've always liked option 3:
char * c
. It avoids the problemschar* c, d;
can cause but still keeps it separate from the name.*
is just likeconst
or any other modifier. You wouldn't writeconstchar* c
so why mash them together just because it's a single character (and allowed)? -
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.
OK, fair enough.
cheers Chris Maunder
-
To remind users that
char* a , b
may not do what they intend. -
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?
When doing more than one variable declaration makes sense to put the * correctly, or you do not get what you expect. When writing: char* a,b,c; you get the equivalent of: char *a,b,c; and you would probably expect: char *a,*b,*c;
-
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?
Because that's how the compiler parses it. The * binds to the variable not the type. Think about char* a, b; This suggests that b is also a char *, but actually it is only a char. Much clearer when you write char *a, b; (Not that I would advocate doing either - even better to have two separate declarations - but it illustrates the point).
Ian Brockbank "Legacy systems are systems that are not protected with a suite of tests. ... You are building legacy code every time you build software without associated tests." - Mary and Tom Poppendieck, Implementing Lean Software Development.
-
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?
If you always puts your declarations on separate lines (or separated by ';') it doesn't matter. But if you do -- be aware. Say that you have; char* c; char* d; char* e; and, for some reason, probably even a good one, decides to put them on one line ... in a bit of hurry so that you ends up with; char* c,d,e; But if you had char *c; char *d; char *e; there is a pretty good chance you would end up with; char *c, *d, *e; This is also the reason for the typedef's of pointers, eg. typedef char * char_p; char_p c; char_p d; char_p e; would be char_p c, d, e; rgds /Jonas ps. char* c, d, e; --> char* c; char d; char e;
-
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.
Great answer. When I define a pointer I'm using `char *p;` because this is pointer. It stores an address and points a `char` value in this address. So, '*' is serving as a pointing device for (p) address register, (I think) it must be declared with address register.
-
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
Great answer. When I define a pointer I'm using char *p; because this is pointer. It stores an address and points a char value in this address. So, '*' is serving as a pointing device for p address register, (I think) it must be declared with address register.
-
If you always puts your declarations on separate lines (or separated by ';') it doesn't matter. But if you do -- be aware. Say that you have; char* c; char* d; char* e; and, for some reason, probably even a good one, decides to put them on one line ... in a bit of hurry so that you ends up with; char* c,d,e; But if you had char *c; char *d; char *e; there is a pretty good chance you would end up with; char *c, *d, *e; This is also the reason for the typedef's of pointers, eg. typedef char * char_p; char_p c; char_p d; char_p e; would be char_p c, d, e; rgds /Jonas ps. char* c, d, e; --> char* c; char d; char e;
-
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?
-
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?
As an aside, in the last century the leading PC C++ compiler vendor was not Microsoft but Borland. One day they got too big for their boots and issued a proclamation which dictated that all users of their IDE must code in their prescribed style - which included suffixing the "*" to the type instead of K&R's prefixing "*" to the variable name. It was at this point that I stopped using Borland. For the sake of consistency, I can't resist also applying the K&R style to references too; although I'm clearly flying in the face of convention from the majority of code examples that I see in books and on-line.
-
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?
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.
-
Why 68? Well, it was a fun language, especially for its time. But the language did't define a concrete syntax at all (there was an Algol68 with keywords in German - fully conformant to the Algol68 standard), so you couldn't use it to settle any concrete syntax arguments. Switching to C# is really a far better solution: Make everything pointers, so that you never say that it is a pointer. If it is an object, then a name of that object is a pointer to it. No way to avoid. That makes it so much simpler, never having to worry about this being a struct, that being a pointer to a struct and something else being a pointer to a pointer to an array of pointers to a struct...
-
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?
Actually, the reason is that 'char*' is not a type. A '*' belongs to the variable. In other words, this is valid C code:
char *pch, ch;
ch = 'A';
pch = &ch;I'm a pure C programmer. I don't know how it works for C++ though.