pointer representation: style or substance?

Z

Zach

Is there any difference between:

char *pointer;
char* pointer;

Is the choice one of aesthetics or does this denote real differences?

Zach
 
L

Lew Pitcher

Is there any difference between:

char *pointer;
char* pointer;

Is the choice one of aesthetics or does this denote real differences?

No real difference; the choice is one of aesthetics only.

However,
char* pointer;
can mislead a neophyte, in that
char* pointer1, pointer2;
may be mis-read as
pointer1 is a pointer to char
pointer2 is a pointer to char
rather than
pointer1 is a pointer to char
pointer2 is a char

IMHO,
char *pointer;
is clearer (to the neophyte), in that there is less chance of
misreading
char *pointer1, pointer2;
as above.

HTH
 
F

Fred Kleinschmidt

Zach said:
Is there any difference between:

char *pointer;
char* pointer;

Is the choice one of aesthetics or does this denote real differences?

There is no difference; it is just a matter of personal style.
However, the second style can be misleading, especially to new
programmers:

char* ptr1, ptr2;

Here, ptr1 is a pointer to char, but ptr1 is just a char.

char *x, y;
The above style makes it more clear that x is a pointer but y is not.
 
S

Servé Laurijssen

Fred Kleinschmidt said:
There is no difference; it is just a matter of personal style.
However, the second style can be misleading, especially to new
programmers:

char* ptr1, ptr2;

Here, ptr1 is a pointer to char, but ptr1 is just a char.

ptr2 you mean :)
 
I

Ian Collins

Fred said:
There is no difference; it is just a matter of personal style.
However, the second style can be misleading, especially to new
programmers:

char* ptr1, ptr2;

Here, ptr1 is a pointer to char, but ptr1 is just a char.

char *x, y;
The above style makes it more clear that x is a pointer but y is not.
The argument becomes moot if you stick to one variable declaration per line.
 
M

Malcolm McLean

Zach said:
Is there any difference between:

char *pointer;
char* pointer;

Is the choice one of aesthetics or does this denote real differences?
C odesn't fuss too much about whitespace.

However char *pointer is better and would be the required form if whitespace
ever became standardised.
You might wonder why * is used both to dereference and to declare a pointer.
When you use the second form to declare, it makes sense in a formal grammary
sort of way.
 
I

Ian Collins

Malcolm said:
C odesn't fuss too much about whitespace.

However char *pointer is better and would be the required form if whitespace
ever became standardised.

Define better!
You might wonder why * is used both to dereference and to declare a pointer.
When you use the second form to declare, it makes sense in a formal grammary
sort of way.

Doesn't this contradict your first paragraph?
 
B

boa

Malcolm said:
C odesn't fuss too much about whitespace.

However char *pointer is better and would be the required form if whitespace
ever became standardised.
You might wonder why * is used both to dereference and to declare a pointer.
When you use the second form to declare, it makes sense in a formal grammary
sort of way.

dmr describes it as "An accident of syntax" in
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html ;-)

Boa
 
C

CBFalconer

Zach said:
Is there any difference between:

char *pointer;
char* pointer;

Is the choice one of aesthetics or does this denote real differences?

In C we normally use the first version. You can see the reason in:

char* a, b, c;

where a is of type char*, while b and c are of type char. This is
also the reason many people (but not me) object to declaring more
than one object per line. If you want all the variable to be of
type char*, you need to declare:

char *a, *b, *c;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
C

Christopher Benson-Manica

Fred Kleinschmidt said:
There is no difference; it is just a matter of personal style.
However, the second style can be misleading, especially to new
programmers:
char* ptr1, ptr2;

Although the specifics are different, the Java IDE I use seems to
agree that multiple definitions per line are a poor stylistic choice.
Brevity may be the soul of wit, but such is not the species of
cleverness demanded of a good programmer.
 
R

Richard Bos

Ian Collins said:
The argument becomes moot if you stick to one variable declaration per line.

True, but then you have to reserve the first sheet of A4 for your
variable declarations.

Richard
 
R

Richard Bos

Ian Collins said:
Not if you keep your functions short.

Well, I may have been exaggerating a little.

However, I do think that

int start_x,start_y,start_z, end_x,end_y,end_z, *tx,*ty,*tz;

is at least as clear as, and a lot more friendly on the browsing than,

int start_x;
int start_y;
int start_z;
int end_x;
int end_y;
int end_z;
int *tx;
int *ty;
int *tz;

IMO, one should group things together which belong together, and keep
things separate which have no connection with one another. This goes for
declarations as much as for nuts and bolts. (As for keeping things
separate, I would not recommend doing this:

int start_x,start_y,start_z, buffersize, age;

That's best split onto three lines.)

Richard
 
K

Keith Thompson

Well, I may have been exaggerating a little.

However, I do think that

int start_x,start_y,start_z, end_x,end_y,end_z, *tx,*ty,*tz;

is at least as clear as, and a lot more friendly on the browsing than,

int start_x;
int start_y;
int start_z;
int end_x;
int end_y;
int end_z;
int *tx;
int *ty;
int *tz;
[...]

I might write that as:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

or maybe even:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

Or, better yet, I might wrap start, end, and t into a struct. If your
code, even a single line of declarations, is long and redundant, often
(but not always) the best solution is to find and exploit the
redundancy an shorten the code.
 
I

Ian Collins

Keith said:
I might write that as:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

or maybe even:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

Or, better yet, I might wrap start, end, and t into a struct. If your
code, even a single line of declarations, is long and redundant, often
(but not always) the best solution is to find and exploit the
redundancy an shorten the code.
A big +1 to that.
 
R

Richard Bos

Keith Thompson said:
Well, I may have been exaggerating a little.

However, I do think that

int start_x,start_y,start_z, end_x,end_y,end_z, *tx,*ty,*tz;

is at least as clear as, and a lot more friendly on the browsing than,

int start_x;
int start_y;
int start_z;
int end_x;
int end_y;
int end_z;
int *tx;
int *ty;
int *tz;
[...]

I might write that as:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

or maybe even:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

Good idea. Even so, you have several declarations on a line. More to the
point, you still need to include the pointer declarators for ty and tx:

int* tx, ty, tz;

would be wrong.

Richard
 
K

Keith Thompson

Keith Thompson said:
(e-mail address removed) (Richard Bos) writes: [...]
I might write that as:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

or maybe even:

int start_x, start_y, start_z;
int end_x, end_y, end_z;
int *tx, *ty, *tz;

Good idea. Even so, you have several declarations on a line. More to the
point, you still need to include the pointer declarators for ty and tx:

int* tx, ty, tz;

would be wrong.

Yes. (That's not a mistake I'd make.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top