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

pointer vs reference

what are the difference between the following 4 variables?

const int * start
int* const start
int const& start
Request(int const& start);
Apr 5 '08 #1
6 1621
Eric Kaplan wrote:
what are the difference between the following 4 variables?
Read right to left.
const int * start
start is a pointer to an int that is constant (the int doesn't change).
int* const start
start is a constant pointer to an int (the pointer doesn't change)
int const& start
start is a reference to a constant int (the int doesn't change).
Request(int const& start);
start is a reference to a constant int (the int doesn't change).

--
Jim Langston
ta*******@rocketmail.com
Apr 5 '08 #2
I suppose the following is exact same thing right?

int * const start
int const * start

both means the pointer is constant - (pointer always point to same
address)
Apr 5 '08 #3
so start is a reference = memory address?

content of start is likely to be something like -
0x12349870h

??
>int const& start

start is a reference to a constant int (the int doesn't change).
Apr 5 '08 #4
Eric Kaplan wrote:
so start is a reference = memory address?

content of start is likely to be something like -
0x12349870h

??
>>int const& start

start is a reference to a constant int (the int doesn't change).
A reference doesn't actually exist. A reference is an alias. The compiler
is free to do that however it wishes. The compiler may actually use the
original variable, or the address, or some other method. You shouldn't
count on how the compiler does it as it may change from compiler to compiler
and even from compiler version to version.

--
Jim Langston
ta*******@rocketmail.com
Apr 5 '08 #5
Eric Kaplan wrote:
I suppose the following is exact same thing right?

int * const start
int const * start

both means the pointer is constant - (pointer always point to same
address)
No. Those are not the same.
const int* start;
int const* start;
are exactly the same.

int* const start;
start is a constant *pointer* to an int. You can not reseat the poniter.
You can not change where the pointer is pointing to. In fact you better
initialize it since you can't change it. But you can change the integer
that the pointer points to.

int const * start;
start is a pointer to a constant *integer* You can not change the value of
what the pointer points to, but you are free to make the pointer point to
some other memory location.
--
Jim Langston
ta*******@rocketmail.com
Apr 5 '08 #6
On 2008-04-04 20:20:30 -0400, "Jim Langston" <ta*******@rocketmail.comsaid:
>
>const int * start

start is a pointer to an int that is constant (the int doesn't change).

>int const& start

start is a reference to a constant int (the int doesn't change).
The value of the int can change, but you can't change it through 'start'.

int i = 0;
const int *start = &i;
std::cout << *start << '\n'; // 0
++i; // OK
std::cout << *start << '\n'; // 1
++*start; // illegal

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Apr 5 '08 #7

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

Similar topics

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...
9
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody...
18
by: man | last post by:
can any one please tell me what is the diff between pointer and reference.....and which one is better to use ....and why???????
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: al.cpwn | last post by:
I get that these two are different int* get() { static int m; return &m; } int& get() {
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
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: 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
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...
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
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.