473,386 Members | 1,715 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,386 software developers and data experts.

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 2512
"Bob Keith" <st*****************@yahoo.com> wrote in message
news:b7*************************@posting.google.co m...
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.google.co m...
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.google.c om>...
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*********@yahoo.com> wrote in message
news:a7**************************@posting.google.c om...
st*****************@yahoo.com (Bob Keith) wrote in message

news:<b7*************************@posting.google.c om>...
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.google.co m...
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.provided> wrote in message
news:c6******************@nasal.pacific.net.au...
Ron Samuel Klatchko <mo*********@yahoo.com> wrote in message
news:a7**************************@posting.google.c om...
st*****************@yahoo.com (Bob Keith) wrote in message news:<b7*************************@posting.google.c om>...
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.net> 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@provided> wrote in message news:<uh******************@nasal.pacific.net.au>.. .
"Jakob Bieling" <ne*****@gmy.net> 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
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...
110
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...
3
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. ...
35
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...
16
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...
204
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 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
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...
69
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...
8
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.