A small clarification ...

O

op

Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
2) int *a; // value stored in a is an integer.

Can we use these interchangeably?

Thanks.
 
E

Emmanuel Delahaye

op a écrit :
Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
Correct.

2) int *a; // value stored in a is an integer.

No. This is stricly equivallent to #1. The blanks (spaces,tabs, end of
lines) are simply ignored.
Can we use these interchangeably?

Yes, or

int*a;
int * a;
int
* a;
int
*
a;

etc.
 
K

kleuske

op schreef:
Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
2) int *a; // value stored in a is an integer.

Not really. The first has a pitfall, though. It suggests the '*' is
part of the type, which isn't correct. Hence it's easy to make the
mistake of typing

int* a, b;

and thinking b is a pointer to int.

If you have the (good) habit of declaring only one var per line, it
does not matter that much. However, whatever you use, consistency is a
benefit in it's own right.
Can we use these interchangeably?

You can, but make up your mind on using one or the other.
 
A

Alex Fraser

op said:
Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
2) int *a; // value stored in a is an integer.

I guess you mean "value stored in *a".
Can we use these interchangeably?

Both contain the same four tokens: "int", "*", "a" and ";" so they are
absolutely equivalent. IME the second form is more common, probably because
"int* a, b;" looks like it declares two pointers but it doesn't.

Alex
 
M

Martin Ambuhl

op said:
Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
2) int *a; // value stored in a is an integer.

There is no difference, and your comments are in error.
In the case of (1), the error is a simple preposition
(a is a pointer *to* where an integer is stored).

For (2), as in (1), the value stored in a is a pointer-to-int.

And note that there are many kinds of integers in C other than 'int', so
your comments in each case tell us less than the declaration does.
Can we use these interchangeably?

Of course. But you don't want to use '//' comments in posting unless
your want your lines munged; nor do you want them in code unless you
have a C99 compiler.
 
R

Richard Heathfield

op said:
Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored

No, a is a pointer which can be used to point to an object of type int.
2) int *a; // value stored in a is an integer.

No, a is a pointer which can be used to point to an object of type int.
 
T

Thad Smith

op schreef:


Not really. The first has a pitfall, though. It suggests the '*' is
part of the type, which isn't correct.

Actually '*' is part of the type of a.
Hence it's easy to make the mistake of typing

int* a, b;

and thinking b is a pointer to int.

True. Even though '*' is part of the type of a, it is part of the
declarator *a and not part of the declarator specifier int.
 
M

Mark McIntyre

Is there any difference between these two ways of declaring a pointer?

1) int* a; // a is a pointer where an integer is stored
2) int *a; // value stored in a is an integer.

There's absolutely no differerence. (The comments are however
misleading).

Both definitions of a are by the way identical to these

int * a;
int* a;
int * a;

Whitespace between the keyword, indirection operator and object name
is irrelevant.
 
J

jat

if u r using only single variable then there is not any difference.

but when u use

int *a, b;

here b will be an integer.and any other variable will be an integer.

but in
int* a,b;

all variable declared will be pointer to integer
 
K

Keith Thompson

jat said:
if u r using only single variable then there is not any difference.

but when u use

int *a, b;

here b will be an integer.and any other variable will be an integer.

but in
int* a,b;

all variable declared will be pointer to integer

Please read <http://cfaj.freeshell.org/google/> and follow its advice.

Please don't use abbreviations like "u" for "you" and "r" for "are".
They just make your text more difficult to read. Proper
capitalization is also helpful.

Finally, you're mistaken. The declarations
int *a, b;
and
int* a,b;
are identical; each declares "a" as a pointer to int and "b" as an
int. Spacing between tokens is ignored by the compiler.

The first version more accurately reflects what's actually going on.
In C, declarations mirror usage; the declaration
int *a;
means that *a is an int, implying that a is an int*.

To avoid any possible misunderstanding (by anyone reading your code,
not by the compiler), declare one variable per line:

int *a;
int b;
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top