If I have a variable called "weight" that contains a float, I can use and set its value by using that name.
Pointers
If - in C or in C++ - I declare a variable to be a
pointer then that variable may contain a memory address ... I use a
* in my type declaration, and then I use
& in my assignment:
float * pweight = &weight;
I can then say that I want to use the contents of that variable by preceeeding the name with
* in my code, for example:
cout << "www " << *pweight << endl;
References
In C++
only, I can also declare a variable to be a
reference by using an
& in my declaration:
float & rweight = weight;
A reference gives a variable a second name, and once it has been assigned the variable can be directly accessed by that new name without any extra characters, for example:
cout << "xxx " << rweight << endl ;
References are a commonly used and useful way of giving a variable a second and temporary name without the undue complexity that's imposed by pointers. There are, however, times that you can't do without pointers!
Here's an example of a complete demonstration program:
#include <iostream>
using namespace std;
int main () {
float weight = 16.8;
/* Pointer */
float * pweight = &weight;
cout << "www " << *pweight << endl;
/* Reference */
float & rweight = weight;
cout << "xxx " << rweight << endl ;
/* Direct Access */
cout << "yyy " << weight << endl ;
/* Each of these alters the same variable */
rweight = rweight + 14.7;
weight = weight + 3.75;
*pweight = *pweight + 4.9;
cout << "zzz " << weight << endl ;
}
Note, specially, how the final three assignments all add to the same variable, but it's referenced in three different ways ... calculating 16.8 + 14.7 + 3.75 + 4.9 when all added together comes to 40.15:
[trainee@easterton lp]$ ./prd
www 16.8
xxx 16.8
yyy 16.8
zzz 40.15
[trainee@easterton lp]$
(written 2009-01-23)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C234 - C and C based languages - Further C++ Object Oriented features [801] Simple polymorphism example - C++ - (2006-07-14)
[802] undefined reference to typeinfo - C++ error message - (2006-07-15)
[831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
[1159] It can take more that one plus one to get two. - (2007-04-22)
[1819] Calling base class constructors - (2008-10-03)
[2004] Variable Scope in C++ - (2009-01-22)
[2576] What does const mean? C and C++ - (2010-01-15)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
[2849] What are C++ references? Why use them? - (2010-07-02)
[3057] Lots of things to do with and within a C++ class - (2010-11-16)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[3124] C++ - putting the language elements together into a program - (2011-01-08)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
[3982] Using a vector within an object - C++ - (2013-01-19)
[4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)
[4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
[4559] When do I use the this keyword in C++? - (2015-10-29)
C207 - C and C based languages - Pointers and references [1155] Pointers in C - (2007-04-19)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1497] Training Season Starts again! - (2008-01-07)
[2572] The what and why of C pointers - (2010-01-13)
[2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3121] New year, new C Course - (2011-01-05)
[3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[3399] From fish, loaves and apples to money, plastic cards and BACS (Perl references explained) - (2011-08-20)
[4128] Allocating memory dynamically in a static language like C - (2013-06-30)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
C201 - C and C based languages - C Language Fundamentals [888] Turning C from source to a running program - (2006-10-06)
[1671] Compiling C programs with gcc - an overview - (2008-06-10)
[2842] Staring a C course with Hello World - why? - (2010-06-30)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3591] Integer types, and integer overflows, in C - (2012-01-25)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4324] Learning to program - variables and constants - (2014-11-22)
[4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
[4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
Some other Articles
The Royal Mail ReceiptThe Month Ahead - What is happening in MelkshamLaunch of Melksham Food and Drink FestivalContrastVariables and pointers and references - C and C++Discount Training Courses - PHP, Perl, PythonNew C Examples - pointers, realloc, structs and moreI have not programmed before, and need to learn2000th article - Remember the background and basics