Click here to Skip to main content
15,894,017 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
Jesse Connell29-May-18 6:03
Jesse Connell29-May-18 6:03 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
jfbode102930-May-18 10:46
jfbode102930-May-18 10:46 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
Jesse Connell30-May-18 17:31
Jesse Connell30-May-18 17:31 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
PNutHed29-May-18 6:05
PNutHed29-May-18 6:05 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
englebart29-May-18 11:27
professionalenglebart29-May-18 11:27 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
ClockMeister29-May-18 13:19
professionalClockMeister29-May-18 13:19 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
david garlisch29-May-18 16:59
david garlisch29-May-18 16:59 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
jfbode102930-May-18 3:58
jfbode102930-May-18 3:58 
Because that's how both the C and C++ grammars work. The asterisk binds to the thing being declared, not the type.

A declaration like
C++
int* a, b;

is parsed as
C++
int (*a), b;


Declarations in C (and simple declarations in C++) are a sequence of declaration specifiers followed by one or more (optionally initialized) declarators. The declarator is what specifies the name along with the the pointer-ness, array-ness, and/or function-ness of the thing being declared. Given a sequence of declaration specifiers D1, then the following are all true:

D *p;    // p is a pointer to D
D a[N];  // a is an array of D
D f();   // f is a function returning D

D *ap[N];  // ap is an array of pointers to D
D *fp();   // fp is a function returning a pointer to D

D (*pa)[N]; // pa is a pointer to an array of D
D (*pf)();  // pf is a pointer to a function returning D 

D (*fpa())[N];  // fpa is a function that returns a pointer to an array of D
D (*apf[N])();  // apf is an array of pointers to functions returning D

// even more complex combinations are possible!!!


The things following D are the declarators. *p is a declarator, as is a[N], as is *ap[N], as is (*pa)[N]. Since both [] and () operators have higher precedence than unary *, an expression like *a[N] parses as *(a[N]). To declare a pointer to an array or a pointer to a function, you must explicitly group the * operator with the thing that points to the array or function.

This is why the common convention (at least among C programmers) is char *c; and not char* c;.

I understand where the type* name and type& name conventions came from in C++, and when I write C++ I follow that convention. But I feel dirty every time I do it.



1. Declaration specifiers include storage class specifiers like static, auto, and typedef, type specifiers like int, double, struct foo, and type qualifiers like const or volatile

modified 30-May-18 12:28pm.

GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
Chad3F30-May-18 8:53
Chad3F30-May-18 8:53 
GeneralRe: Why does most C/C++ developer prefers char *c instead of char* c? Pin
code_junkie31-May-18 3:52
code_junkie31-May-18 3:52 
GeneralLife back to normal Pin
lopatir25-May-18 22:52
lopatir25-May-18 22:52 
GeneralRe: Life back to normal Pin
RickZeeland26-May-18 0:17
mveRickZeeland26-May-18 0:17 
GeneralRe: Life back to normal Pin
OriginalGriff26-May-18 0:31
mveOriginalGriff26-May-18 0:31 
GeneralRe: Life back to normal Pin
RickZeeland26-May-18 0:37
mveRickZeeland26-May-18 0:37 
GeneralRe: Life back to normal Pin
OriginalGriff26-May-18 1:10
mveOriginalGriff26-May-18 1:10 
GeneralRe: Life back to normal Pin
lopatir26-May-18 2:27
lopatir26-May-18 2:27 
GeneralLow profile Pin
RickZeeland25-May-18 20:30
mveRickZeeland25-May-18 20:30 
GeneralRe: Low profile Pin
CPallini25-May-18 21:14
mveCPallini25-May-18 21:14 
GeneralRe: Low profile Pin
RickZeeland25-May-18 22:17
mveRickZeeland25-May-18 22:17 
GeneralRe: Low profile Pin
CPallini25-May-18 22:47
mveCPallini25-May-18 22:47 
GeneralRe: Low profile Pin
OriginalGriff25-May-18 21:32
mveOriginalGriff25-May-18 21:32 
GeneralRe: Low profile Pin
RickZeeland25-May-18 22:21
mveRickZeeland25-May-18 22:21 
JokeRe: Low profile Pin
Daniel Pfeffer26-May-18 9:00
professionalDaniel Pfeffer26-May-18 9:00 
GeneralRe: Low profile Pin
RickZeeland2-Jun-18 17:43
mveRickZeeland2-Jun-18 17:43 
GeneralYour pair-programmer's code causes massive server failure: you get fired ? Pin
BillWoodruff25-May-18 16:39
professionalBillWoodruff25-May-18 16:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.