References, Pointers and Constants: Difference between revisions

From Yggenyk
Jump to navigation Jump to search
(New page: ==Simple C++ Data Types== {|width="100%" border="1" cellpadding="2" cellspacing="0" |- !style="color:#ffb900"|C++ Code !style="color:#ffb900"|What It Means !style="color:#ffb900"|When To ...)
 
No edit summary
Line 39: Line 39:
|-
|-
|
|
const char []<br>
const int []<br>
const double []<br>
|
|
Constant array of values<br>
(const char [] is a string constant)
|
|
To declare an array of constant values (no element in the array can be changed)<br>
const char [] is used to declare a string constant
|-
|-
|
|
char *<br>
int *<br>
double *
|
|
Pointer to a single value or an array of values
|
|
To store a string (char *)<br>
To store an array of values when you have to allocate the memory dynamically (using new)<br>
To pass an array to a function<br>
To pass a value to a function which the function will set (passing by reference)<br>
To return a dynamically-allocated array from a function.<br>
|-
|-
const char *<br>
const int *<br>
const double *
|
|
Pointer to a constant value
|
|
When you don't want the value at the pointer to be changed, but you can still change the pointer<br>
To return a pointer to a constant value<br>
To pass a pointer to a constant value to a function
|
|
|-
|-
|
|
char &<br>
int &<br>
double &
|
|
Reference to a value
|
|
To pass a value to a function which will be set by the function (without using a pointer)
|-
|-
char * &<br>
int * &<br>
double * &
|
|
Reference to a pointer
|
|
To pass a pointer to a function where the function will set the pointer (either by calling new or by assigning to the pointer
|
|-
char * const<br>
int * const<br>
double * const
|
Constant pointer
|
When you want to point to a specific value.  Allows to you to change the data that is pointed to, but you can't change the pointer
|
|
|}
|}

Revision as of 07:24, 14 October 2008

Simple C++ Data Types

const char *
const int *
const double * char * &
int * &
double * & char * const
int * const
double * const
C++ Code What It Means When To Use It

char
int
double

Single (scalar) value (character, integer, floating point number)

To store data
To pass data to a function by value
To return data by value

const char
const int
const double

Constant value

To declare a constant -- a value that cannot be changed
Has no effect for passing values to a function or from returning values from a function

char []

int []
double []

Array (vector) of values
(char [] is a string)

To store the same type of data together
To pass an array to a function

const char []
const int []
const double []

Constant array of values
(const char [] is a string constant)

To declare an array of constant values (no element in the array can be changed)
const char [] is used to declare a string constant

char *
int *
double *

Pointer to a single value or an array of values

To store a string (char *)
To store an array of values when you have to allocate the memory dynamically (using new)
To pass an array to a function
To pass a value to a function which the function will set (passing by reference)
To return a dynamically-allocated array from a function.

Pointer to a constant value

When you don't want the value at the pointer to be changed, but you can still change the pointer
To return a pointer to a constant value
To pass a pointer to a constant value to a function

char &
int &
double &

Reference to a value

To pass a value to a function which will be set by the function (without using a pointer)

Reference to a pointer

To pass a pointer to a function where the function will set the pointer (either by calling new or by assigning to the pointer

Constant pointer

When you want to point to a specific value. Allows to you to change the data that is pointed to, but you can't change the pointer

id=siteTree