På yggenyk.dk bruger vi cookies til at give dig en god oplevelse og til at indsamle statistik, der kan være med til at forbedre brugeroplevelsen. Hvis du klikker på et link på yggenyk.dk, accepterer du samtidig vores cookiepolitik.
References, Pointers and Constants: Difference between revisions
No edit summary |
|||
Line 26: | Line 26: | ||
To declare a constant -- a value that cannot be changed<br> | To declare a constant -- a value that cannot be changed<br> | ||
Has no effect for passing values to a function or from returning values from a function | Has no effect for passing values to a function or from returning values from a function | ||
|- | |- | ||
|char []<br> | | | ||
char []<br> | |||
int []<br> | int []<br> | ||
double [] | double [] | ||
Line 62: | Line 62: | ||
To return a dynamically-allocated array from a function.<br> | To return a dynamically-allocated array from a function.<br> | ||
|- | |- | ||
| | |||
const char *<br> | const char *<br> | ||
const int *<br> | const int *<br> | ||
Line 71: | Line 72: | ||
To return a pointer to a constant value<br> | To return a pointer to a constant value<br> | ||
To pass a pointer to a constant value to a function | To pass a pointer to a constant value to a function | ||
|- | |- | ||
| | | | ||
Line 82: | Line 82: | ||
To pass a value to a function which will be set by the function (without using a pointer) | To pass a value to a function which will be set by the function (without using a pointer) | ||
|- | |- | ||
| | |||
char * &<br> | char * &<br> | ||
int * &<br> | int * &<br> | ||
Line 89: | Line 90: | ||
| | | | ||
To pass a pointer to a function where the function will set the pointer (either by calling new or by assigning to the 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> | char * const<br> | ||
int * const<br> | int * const<br> | ||
Line 98: | Line 99: | ||
| | | | ||
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 | 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 | ||
| | |} | ||
{|width="100%" border="1" cellpadding="2" cellspacing="0" | |||
|- | |||
!style="color:#ffb900"|C++ Code | |||
!style="color:#ffb900"|What It Means | |||
!style="color:#ffb900"|When To Use It | |||
|- | |||
|1 | |||
|2 | |||
|3 | |||
|} | |} |
Revision as of 07:30, 14 October 2008
Simple C++ Data Types
C++ Code | What It Means | When To Use It |
---|---|---|
char |
Single (scalar) value (character, integer, floating point number) |
To store data |
const char |
Constant value |
To declare a constant -- a value that cannot be changed |
char [] |
Array (vector) of values |
To store the same type of data together |
const char [] |
Constant array of values |
To declare an array of constant values (no element in the array can be changed) |
char * |
Pointer to a single value or an array of values |
To store a string (char *) |
const char * |
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 |
char & |
Reference to a value |
To pass a value to a function which will be set by the function (without using a pointer) |
char * & |
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 |
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 |
C++ Code | What It Means | When To Use It |
---|---|---|
1 | 2 | 3 |