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

A small clarification ...

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

Dec 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a

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.

Dec 17 '05 #2

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

--
A+

Emmanuel Delahaye
Dec 17 '05 #3

P: n/a
"op" <op******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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
Dec 17 '05 #4

P: n/a
op wrote:
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.
Dec 17 '05 #5

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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Dec 17 '05 #6

P: n/a
kl*****@xs4all.nl wrote:

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.


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.

--
Thad
Dec 17 '05 #7

P: n/a
On 17 Dec 2005 01:29:29 -0800, in comp.lang.c , "op"
<op******@gmail.com> wrote:
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.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '05 #8

P: n/a
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

Dec 19 '05 #9

P: n/a
"jat" <co**************@gmail.com> writes:
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;

--
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.
Dec 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.