char *fred; char * fred; char *fred; any difference?

B

Ben Pfaff

Hal Styli said:
can anyone explain the difference between the above declarations and why
anyone would stray from the first.
Are the other 2 standard, couldnt find them in K&R2.

I suppose you're referring to the subject of your article, which
is
char *fred; char * fred; char *fred; any difference?
These all have the same meaning, as does char*fred.
 
H

Hal Styli

Hello,

can anyone explain the difference between the above declarations and why
anyone would stray from the first.
Are the other 2 standard, couldnt find them in K&R2.

Thanks,
Hal.
 
M

Mark McIntyre

Hello,

can anyone explain the difference between the above declarations

what "above" declarations??? There's no declarations in your post.

(In other words, don't put your question in the subject, put it in the
mail body. Not all mail readers display teh subject next to the body,

Anyway all three are identical. As is
char * fred;
because whitespace is totally immaterial in (most) C statements.
and why anyone would stray from the first.

C++ programmers prefer to attach the * to the type. C programmers to
the object. Either is acceptable.
Are the other 2 standard, couldnt find them in K&R2.

K&R is a C book, and the authors used their preferred style.
 
K

Kenneth Brody

Mark McIntyre wrote:
[...]
Anyway all three are identical. As is
char * fred;
because whitespace is totally immaterial in (most) C statements.


C++ programmers prefer to attach the * to the type. C programmers to
the object. Either is acceptable.

In the case of a single variable, yes. Of course:

char *ptr,c;

is probably clearer than:

char* ptr,c;

But, the above are still identical in meaning, and it's mostly a
matter of personal preference. (Sort of like "where do I put the
braces for for/while/etc. statements?")

[...]

--

+---------+----------------------------------+-----------------------------+
| Kenneth | kenbrody at spamcop.net | "The opinions expressed |
| J. | http://www.hvcomputer.com | herein are not necessarily |
| Brody | http://www.fptech.com | those of fP Technologies." |
+---------+----------------------------------+-----------------------------+
 
C

CBFalconer

Kenneth said:
Mark McIntyre wrote:
[...]
C++ programmers prefer to attach the * to the type.
C programmers to the object. Either is acceptable.

In the case of a single variable, yes. Of course:

char *ptr,c;

is probably clearer than:

char* ptr,c;

But, the above are still identical in meaning, and it's mostly
a matter of personal preference. (Sort of like "where do I put
the braces for for/while/etc. statements?")

C programmers prefer "char *ptr" because, while " char *"
describes the type pointer to char, it can't be used for multiple
items due to Cs weird and wonderful declaration syntax. However
you can use (but often frowned upon as obfuscative):

typedef char *charptr;
charptr cp1, cp2, cp3;

which has a much different meaning for cp2 and cp3 than:

char * cp1, cp2, cp3;
 
T

Tristan Miller

Greetings.

can anyone explain the difference between the above declarations and why
anyone would stray from the first.
Are the other 2 standard, couldnt find them in K&R2.

Many people prefer keeping the * next to the variable rather than the type,
as this avoids the implication that the * is distributive. However,
syntactically the position doesn't matter.

Regards,
Tristan
 

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

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top