473,399 Members | 3,603 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,399 software developers and data experts.

pointer in C++

I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a
int &a
int& a
int*& a

Many thanks.
Jul 19 '05 #1
6 2997
"Bob Keith" <st*****************@yahoo.com> wrote in message
news:b7**************************@posting.google.c om...
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a // a is a pointer to an int
int *a // a is again a pointer to an int
int &a // a is a reference to an int
int& a // a is again a reference to an int
int*& a // a is a reference to a pointer to an int
HTH,

Stuart.
Many thanks.

Jul 19 '05 #2
"Bob Keith" <st*****************@yahoo.com> wrote...
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a
The two declarations above are the same and declare 'a'
a pointer to an int.
int &a
int& a
The two declarations above are the same and declare 'a'
a reference to an int (no pointers here).
int*& a
This declaration declares 'a' a reference to a pointer to
an int.

All three last declarations require initialisation if they
are also definitions. Sounds convoluted? It is, sort of.
If your declaration is in the namespace scope or in a function
scope, they are also definitions. And any definition of
a reference requires an initialiser.

int *& rpa; // error - lacks initialiser (namespace scope)
int main()
{
int& a; // error - lacks initialiser (function scope)
}

Many thanks.


Many welcomes.

Victor
Jul 19 '05 #3

"Bob Keith" <st*****************@yahoo.com> wrote in message news:b7**************************@posting.google.c om...
I am a beginner. So this question could be very stupid.

I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
int* a
int *a
int &a
int& a
int*& a

Half of those aren't pointers.
The arrangement of the white space is not significant.
Jul 19 '05 #4
Sin
> I am not very clear about the pointer in C++, would anyone let me know
the difference of the following usage of pointer.
Sure

int* a
int *a
Same exact thing. Should it be on a, or on int? It doensn't change a thing
as far as the compiler is concerned and for clarity there are arguments on
both sides. The important thing to know is that "int *a, b;" will declare a
int pointer (a) and an integer (b), rather than two pointers...

As for what a pointer is, it's simply a placeholder for an address...

Ex:

int v= 12;
int *a= &v;
int *b= &v
int *c= &v;

*a= 13;
// At this point, v == 13, *a == 13, *b == 13 and *c == 13

int &a
int& a
Again, both are the same. They are NOT pointers though they are references.
A reference is similar to a pointer, but it's actually just a variable that
shares the same address as the object it's initialized with. It makes their
use easier for the non initiated, I guess, but it lacks the possibility of
being set to nothing (NULL).

Ex:

int v= 12;
int &a= v;
int &b= v;
int &c= v;

a= 13;
// At this point, v == 13, a == 13, b == 13 and c == 13

int*& a


That would be a reference to a pointer...

Ex:

int v= 12;
int &r= v;
int *p= &r;
int *&rp= p;

*rp= 13;

// At this point, v == 13, r == 13, *p == 13, **rp == 13
In my opinion references should be avoided unless necessary. I mostly do C
with occasionnal C++ and I can count on the fingers of one hand the times I
had to use references in the past 5 years...

Alex.
Jul 19 '05 #5

"Howard" <al*****@hotmail.com> wrote in message news:bd********@dispatch.concentric.net...

"Ron Natalie" <ro*@sensor.com> wrote in message
news:Kd********************@giganews.com...

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

Half of those aren't pointers.


Half? So, say, 2.5 of those 5 variables are (or are not) pointers? Perhaps
this is new math? :-)


Sure, the reference to a pointer counts as half :-)
Jul 19 '05 #6
"Yuh-Horng Shiau" <yh*****@mail.nuu.edu.tw> wrote in message
news:3F**************@mail.nuu.edu.tw...
The declaration of 'int *& a;' will get an error from the compiler.


'int*& a;' will give you an error yes, because you are not initializing
the reference. Just like 'int& a;' will give you an error. The following is
valid:

int* b = 0;
int*& a = b;

regards
--
jb

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

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
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: 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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...

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.