Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Variables and pointers and references - C and C++

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 14:58:49)

 
Associated topics are indexed under
C201 - C and C based languages - C Language Fundamentals
  [2576] What does const mean? C and C++ - (2010-01-15)
  [1671] Compiling C programs with gcc - an overview - (2008-06-10)
  [888] Turning C from source to a running program - (2006-10-06)

C207 - C and C based languages - Pointers and references
  [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
  [2572] The what and why of C pointers - (2010-01-13)
  [1497] Training Season Starts again! - (2008-01-07)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1155] Pointers in C - (2007-04-19)


Back to
Variable Scope in C++
Previous and next
or
Horse's mouth home
Forward to
Contrast
Some other Articles
The Royal Mail Receipt
The Month Ahead - What is happening in Melksham
Launch of Melksham Food and Drink Festival
Contrast
Variables and pointers and references - C and C++
Variable Scope in C++
Discount Training Courses - PHP, Perl, Python
New C Examples - pointers, realloc, structs and more
I have not programmed before, and need to learn
2000th article - Remember the background and basics
2674 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2010: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 0800 043 8225 or 01225 708225 • FAX: 01225 344596 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho