Problem with a declaration

L

luserXtrog

What's wrong with this declaration?

char* p1, p2;

I get errors when I try to use p2.

Dude! If you're for real, read the FAQ.
This and your previous question are frequently asked
and definitively answered in the Frequently-Asked-Questions.

In the (foredoomed) interest of capping this baloney,
here's the answer anyway.
You're mistakenly assuming that char* applies to p2.
The compiler ignores your spacing and interprets the
declaration more like this:

char (*p1), (p2);

See? p2 has no star! It is not declared to be a pointer,
but just a char variable.

So put a * in front of p2; and move the other star next
to the p1 so you don't confuse yourself again.

hth
 
P

Phil Carmody

C man said:
What's wrong with this declaration?

char* p1, p2;

Nothing apart from style issues.
I get errors when I try to use p2.

Then you're using it wrong.

char *p1, p2;
//...
p2 = *p1;

However, if you were trying to have mnemonic variable names,
I'd have thought that 'c1' would be a better name than 'p2'
for the variable of type char.

Phil
 
B

Ben Bacarisse

Ian Collins said:
Don't declare more than one variable on a line and you won't hit
this FAQ.

Alternatively, write *all* your declarations in as few lines as
possible, for a while at least, until you are entirely comfortable
with the syntax. Since you are certain to come across this form of
declaration from time to time it is worth being sure you understand
it, even if you later choose never to write any of them yourself.
 
G

gw7rib

Why would you try to be super clever rather than just explain that his
declaration was wrong : understandable mistake but wrong?

Perhaps he thought the OP was having him on, seeing as the question
posted is word for word the same as it appears in the FAQ.
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top