473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer problem in C++

Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

int *a
int* a
int** a
int*& a
int *&a
int &a

Many thanks.
Jul 19 '05 #1
8 2526
"Bob Keith" <st************ *****@yahoo.com > wrote in message
news:b7******** *************** **@posting.goog le.com...
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

int *a
int* a
Both are the same. 'a' is a pointer to an int.
int** a
'a' is a pointer to a pointer to an int.
int*& a
int *&a
Again, those two are the same. 'a' is a reference to a pointer to an
int.
int &a


And here 'a' is just a reference to an int.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

"Bob Keith" <st************ *****@yahoo.com > wrote in message
news:b7******** *************** **@posting.goog le.com...
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.

int *a
int* a
int** a
int*& a
int *&a
int &a

Many thanks.


Didn't you read the replies to your post last week?

Jul 19 '05 #3
st************* ****@yahoo.com (Bob Keith) wrote in message news:<b7******* *************** ***@posting.goo gle.com>...
int *a
int* a
int** a
int*& a
int *&a
int &a


Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise) while others
prefer the latter because the * modifies the type. Just pick one.
When I critique people, I don't care which one they use as long as
they are consistent.

The second issue that looks to be confusing you is references and
pointers. That is normal because the two are quite similiar and all
the implementations I know (which admittedly is not huge) implement
references via pointers.

Both pointers and references can "reference" another variable. But
they do have some differences.

1) Pointers use 'special' syntax while references use standard object
syntax. To access a field of a pointer, you use the -> operator you
use the . operator to access a field of a reference.

2) References must always be bound to a variable while pointers can
legally point to nothing.

My general recommendation is to start by only using references as
formal parameters to a function. Don't bother trying to fully
understand references until that feels comfortable.

samuel
Jul 19 '05 #4
Ron Samuel Klatchko <mo*********@ya hoo.com> wrote in message
news:a7******** *************** ***@posting.goo gle.com...
st************* ****@yahoo.com (Bob Keith) wrote in message

news:<b7******* *************** ***@posting.goo gle.com>...
int *a
int* a
int** a
int*& a
int *&a
int &a


Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise)


Rather than the rules of declaring multiple variables, it is likely to be
because of the rules of the language. To the compiler, the * is bound to the
identifier, not the type. The "rules of declaring of multiple variables" are
simply a consequence of that. If you write "int* x,y" the compiler sees the
type as 'int' and the ints being declared as *x and y.

To the OP, the declaration rules are designed to mimic expressions.

int k;
This says that where 'k' appears in an expression, its type is 'int', e.g.,
k = 1;
k = k + 3;

int *pk = &k;
This says that where *pk appears in an expression, its type is also 'int'.
The * is the dereference-pointer operator. Therefore 'pk' is a pointer to an
int, and *pk is the int that 'pk' points to, in this case 'k'.

*pk = *pk + 1;

Since *pk is an int, this expression takes the int at *pk, adds 1 to it, and
stores it back to *pk.

Some people think it's silly to think of "int *pk" as "declare *pk to be of
type int", even though that's what it was designed to mean. Those people
prefer to think "declare pk to be of type pointer-to-int". That's why they
write "int* pk".

Note that it is only an accident of history that the * was placed on the
left of the identifier and not the right, which enables you to move the *
across the whitespace to the type name. There are many other declarations in
which you can't do that, e.g.,

int a[10];
Array of 10 ints. (You can't have "int[10] a;")

int (*pf)();
Pointer to a function returning an int (i.e., where (*pf)() appears in an
expression its type is 'int')

int (*pa)[10];
Pointer to an array of 10 ints (i.e., where (*pa)[i] appears in an
expression, its type is 'int')

Make your own choice.

David

Jul 19 '05 #5
"Bob Keith" <st************ *****@yahoo.com > wrote in message
news:b7******** *************** **@posting.goog le.com...
Hello, all:

I am a beginner of C++, and feel confusing about the differences of
the following terms regarding pointer.
Perhaps you would be less confused if you'd read the answer I gave to your
post less than a week ago which bore a remarkable resemblance to this one.
Please read the replies you get this time.
int *a
a is a pointer to an int
int* a
a is a pointer to an int
int** a
a is a pointer to a pointer to an int
int*& a
a is a reference to a pointer to an int
int *&a
a is a reference to a pointer to an int
int &a
a is a reference to an int
Many thanks.


You're welcome... :-) And I recommend the following books, since asking for
book recommendations was implicit in your post:

The C++ Programming Language (Stroustrup)
C++ Primer (Lippmann and Lajoie)
Accelerated C++ (Koenig and Moo)

HTH,

Stuart.
Jul 19 '05 #6
"David White" <no@email.provi ded> wrote in message
news:c6******** **********@nasa l.pacific.net.a u...
Ron Samuel Klatchko <mo*********@ya hoo.com> wrote in message
news:a7******** *************** ***@posting.goo gle.com...
st************* ****@yahoo.com (Bob Keith) wrote in message news:<b7******* *************** ***@posting.goo gle.com>...
int *a
int* a
int** a
int*& a
int *&a
int &a


Trying to read between the lines here, I'm guessing there are two
things that are confusing you.

First, to knock off the easy one, let's talk about whitespace. In
general, where whitespace goes is irrelevant (although not always).
So there is no difference between "int *a" and "int* a". Some people
prefer the former because of the rules of declaring multiples
variables (and int* x,y declares x as a pointer to an integer and y as
an integer, but the placement of the * implies otherwise)


Rather than the rules of declaring multiple variables, it is likely to be
because of the rules of the language. To the compiler, the * is bound to

the identifier, not the type. The "rules of declaring of multiple variables" are simply a consequence of that. If you write "int* x,y" the compiler sees the type as 'int' and the ints being declared as *x and y.

To the OP, the declaration rules are designed to mimic expressions.

int k;
This says that where 'k' appears in an expression, its type is 'int', e.g., k = 1;
k = k + 3;

int *pk = &k;
This says that where *pk appears in an expression, its type is also 'int'.
The * is the dereference-pointer operator. Therefore 'pk' is a pointer to an int, and *pk is the int that 'pk' points to, in this case 'k'.

*pk = *pk + 1;

Since *pk is an int, this expression takes the int at *pk, adds 1 to it, and stores it back to *pk.

Some people think it's silly to think of "int *pk" as "declare *pk to be of type int", even though that's what it was designed to mean. Those people
prefer to think "declare pk to be of type pointer-to-int". That's why they
write "int* pk".

Note that it is only an accident of history that the * was placed on the
left of the identifier and not the right, which enables you to move the *
across the whitespace to the type name. There are many other declarations in which you can't do that, e.g.,

int a[10];
Array of 10 ints. (You can't have "int[10] a;")

int (*pf)();
Pointer to a function returning an int (i.e., where (*pf)() appears in an
expression its type is 'int')

int (*pa)[10];
Pointer to an array of 10 ints (i.e., where (*pa)[i] appears in an
expression, its type is 'int')

Make your own choice.


http://www.research.att.com/~bs/bs_faq2.html#whitespace

regards
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #7
"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:bd******** *****@news.t-online.com...

http://www.research.att.com/~bs/bs_faq2.html#whitespace


Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
serious doubts that C and C++ programmers really see pointer declarations
differently. I usually think of 'p' in "int *p" as a "pointer to int" as
well, and I came to C++ from C. The only time I really think of it as "*p is
an int" is where *p appears in an expression and when I explain the syntax
rules in places like this. But that has no influence on how I write
declarations, simply because to write "int* p" is a special case. You simply
can't declare a pointer to a function, for example, in such a way that you
can have the type information in one place and the identifier somewhere
else, with whitespace in between. I hate special cases. I prefer to follow
the rules that the compiler follows, not fight against them and be
inconsistent in the process.

I also suspect that having an "int *p" camp and an "int* p" camp confuses
the hell out of people trying to learn the language. I don't believe that
you explain the declaration syntax rules well enough for a student to
understand any declaration without the "int* p" style appearing to
contradict the words used to explain the rules.

David

Jul 19 '05 #8
"David White" <no.email@provi ded> wrote in message news:<uh******* ***********@nas al.pacific.net. au>...
"Jakob Bieling" <ne*****@gmy.ne t> wrote in message
news:bd******** *****@news.t-online.com...

http://www.research.att.com/~bs/bs_faq2.html#whitespace


Thanks. Interesting, but I don't entirely agree with Stroustrup. I have
serious doubts that C and C++ programmers really see pointer declarations
differently. I usually think of 'p' in "int *p" as a "pointer to int" as
well, and I came to C++ from C. The only time I really think of it as "*p is
an int" is where *p appears in an expression and when I explain the syntax
rules in places like this. But that has no influence on how I write
declarations, simply because to write "int* p" is a special case. You simply
can't declare a pointer to a function, for example, in such a way that you
can have the type information in one place and the identifier somewhere
else, with whitespace in between. I hate special cases. I prefer to follow
the rules that the compiler follows, not fight against them and be
inconsistent in the process.

I also suspect that having an "int *p" camp and an "int* p" camp confuses
the hell out of people trying to learn the language. I don't believe that
you explain the declaration syntax rules well enough for a student to
understand any declaration without the "int* p" style appearing to
contradict the words used to explain the rules.


"int *p" confused the hell out of me until I realised I could write it
as "int* p". Just the way I think about things I suppose. Admittedly,
I learnt these things as they came up, rather than someone formally
explaining the full set of declaration syntax rules in one go.

I understand your point about "int* p" being a special case that
cannot be applied to function pointers. But when I later encountered
function pointers, I found that there was so much more syntax going on
in the declarations that the inconsistency in the position of the *
was the least of my worries. I could cope with that, it was all the
parentheses all over the place that bothered me :)

I would still be much happier if I could completely separate type from
variable name, something like

void (*)(int) pf;

instead of

void (*pf)(int);

At the end of the article, Stroustrup says "Whenever something can be
done in two ways, someone will be confused. Whenever something is a
matter of taste, discussions can drag on forever." Sounds to me like a
convincing argument for not having things that can be done two ways as
a matter of taste.

GJD
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2124
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
110
9814
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
3
2334
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2863
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2272
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an...
204
12927
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
16
2488
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
7777
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
69
5510
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after...
8
2219
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3459
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.