By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

pointer representation: style or substance?

P: n/a
Is there any difference between:

char *pointer;
char* pointer;

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

Zach

Feb 19 '07 #1
Share this Question
Share on Google+
23 Replies


P: n/a
On Feb 19, 1:06 pm, "Zach" <net...@gmail.comwrote:
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
--
Lew

Feb 19 '07 #2

P: n/a
On Feb 19, 1:13 pm, "Lew Pitcher" <lpitc...@sympatico.cawrote:
>
No real difference; the choice is one of aesthetics only.
Thanks Lew.

Zach
Feb 19 '07 #3

P: n/a

"Zach" <ne****@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
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.

--
Fred L. Kleinschmidt
Feb 19 '07 #4

P: n/a

"Fred Kleinschmidt" <fr******************@boeing.comwrote in message
news:JD********@news.boeing.com...
>
"Zach" <ne****@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
>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.
ptr2 you mean :)

Feb 19 '07 #5

P: n/a
Fred Kleinschmidt wrote:
"Zach" <ne****@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
>>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.
The argument becomes moot if you stick to one variable declaration per line.

--
Ian Collins.
Feb 19 '07 #6

P: n/a

"Zach" <ne****@gmail.comwrote in message
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.
>

Feb 19 '07 #7

P: n/a
Malcolm McLean wrote:
"Zach" <ne****@gmail.comwrote in message
>>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.
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?

--
Ian Collins.
Feb 19 '07 #8

P: n/a
boa
Malcolm McLean wrote:
"Zach" <ne****@gmail.comwrote in message
>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.
dmr describes it as "An accident of syntax" in
http://cm.bell-labs.com/cm/cs/who/dmr/chist.html ;-)

Boa
Feb 19 '07 #9

P: n/a
Zach wrote:
>
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
Feb 20 '07 #10

P: n/a
Fred Kleinschmidt <fr******************@boeing.comwrote:
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 20 '07 #11

P: n/a
Ian Collins <ia******@hotmail.comwrote:
Fred Kleinschmidt wrote:
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.
True, but then you have to reserve the first sheet of A4 for your
variable declarations.

Richard
Feb 20 '07 #12

P: n/a
Richard Bos wrote:
Ian Collins <ia******@hotmail.comwrote:

>>Fred Kleinschmidt wrote:
>>>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.

True, but then you have to reserve the first sheet of A4 for your
variable declarations.
Not if you keep your functions short.

--
Ian Collins.
Feb 20 '07 #13

P: n/a
Ian Collins <ia******@hotmail.comwrote:
True, but then you have to reserve the first sheet of A4 for your
variable declarations.
Not if you keep your functions short.
IMHO, a function with enough variables to fill a sheet of A4 is in need
of refactoring.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Feb 20 '07 #14

P: n/a
On Feb 20, 4:30 am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
>
True, but then you have to reserve the first sheet of A4 for your
variable declarations.
Hoi Richard,

Or for us Americans the first sheet of Letter ;-)

Zach

Feb 21 '07 #15

P: n/a
Ian Collins <ia******@hotmail.comwrote:
Richard Bos wrote:
Ian Collins <ia******@hotmail.comwrote:
>Fred Kleinschmidt wrote:

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.
True, but then you have to reserve the first sheet of A4 for your
variable declarations.
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
Feb 22 '07 #16

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #17

P: n/a
Keith Thompson wrote:
>
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.

--
Ian Collins.
Feb 22 '07 #18

P: n/a
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
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
Feb 23 '07 #19

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
>rl*@hoekstra-uitgeverij.nl (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.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 23 '07 #20

P: n/a
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
rl*@hoekstra-uitgeverij.nl (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.)
Well, no, because you use (as evidenced above) the Right Way of writing
declarations, not the Wrong Way.

(Your turn to recycle this thread, Ian.)

Richard
Feb 26 '07 #21

P: n/a
Richard Bos wrote:
Keith Thompson <ks***@mib.orgwrote:

>>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
>>>Keith Thompson <ks***@mib.orgwrote:

rl*@hoekstra-uitgeverij.nl (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.)


Well, no, because you use (as evidenced above) the Right Way of writing
declarations, not the Wrong Way.

(Your turn to recycle this thread, Ian.)
The problem would exist if you only put one declaration per line!

--
Ian Collins.
Feb 26 '07 #22

P: n/a
Ian Collins wrote:
Richard Bos wrote:
>>Keith Thompson <ks***@mib.orgwrote:
>>>rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
Keith Thompson <ks***@mib.orgwrote:
>rl*@hoekstra-uitgeverij.nl (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.)


Well, no, because you use (as evidenced above) the Right Way of writing
declarations, not the Wrong Way.

(Your turn to recycle this thread, Ian.)

The problem would exist if you only put one declaration per line!
Oops, too early - "The problem would not exist"

--
Ian Collins.
Feb 26 '07 #23

P: n/a
Keith Thompson wrote:
>
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
[...]
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;
I'll choose

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

--
DPS
Feb 28 '07 #24

This discussion thread is closed

Replies have been disabled for this discussion.