473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A small clarification ...

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
9 1179

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
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
"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
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
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wes | last post by:
I am trying to secure different files, mostly pdf, so only the person suppose to see the file that was designed for that individual can see it. I am using sessions to secure the actual web pages,...
4
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a...
3
by: John D. Sanders | last post by:
I have just upgraded MySQL from version 3.23 to version 4.1.8 and now I am getting the following error when I try to run a script that worked: install_driver(mysql) failed: Can't load...
2
by: Ethan | last post by:
This is a clarification of a previous message, which was not expressed very well. I have a set of checkboxes near the bottom of the page and a function that checks or unchecks all of them. But when...
9
by: Adam | last post by:
Hi, I am having problems having an include file rendered in my browser (IE6). The include file contains <A href> tags to be used as a navigation bar in the footer of a .html file. I am...
3
by: ma740988 | last post by:
Consider the 'C' source. void myDoorBellISR(starLinkDevice *slDevice, U32 doorBellVal) { doorBellDetected = doorBellVal; } void slRcv() { starLinkOpenStruct myOpenStruct;
3
by: solomon_13000 | last post by:
> Wonthaggi Civic Theatre 'WCT' Case Study > > The town of Wonthaggi has a theatre which is owned and > operated by the local council, it is called the > Wonthaggi Civic Theatre (WCT) and a wide...
0
by: chanchito_cojones | last post by:
Hi there, I was searching the net for some guidance in putting together a query that would select random records from the main table. I came across this and it works like a charm. SELECT TOP...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.