Pointers - itzjac/cpplearning GitHub Wiki
Array parameters
Can't copy an array is usually converted to a pointer.
This functions are equivalent
void function(const int *arr);
void function(const int arr[]); // shows the intent that the function takes an array
void function(const int arr[10]); // dimension for documentation purposes (at best)
Pointer arithmetic for arrays.
Use unary operators.
Derreference values
void PrintArray(int array[20])
{
std::cout << "sizeof(array) = " << sizeof(array) << std::endl; // it will print only 4 bytes
}
int main()
{
srand( time(0) );
int randomArray[20];
for (int i = 0; i < 20; ++i)
{
randomArray[i] = rand() % 101;
}
std::cout << "sizeof(randomArray) = " << sizeof(randomArray) << std::endl; // it will print 80 = 20 elementes each of size 4 bytes
PrintArray(randomArray);
}
Check array bounds
-
Character arrays, end marker (NULL)
-
Integer arrays
void function(const int* beg, const int *end) { while(beg != end) std::cout<< *beg++<
-
Indicate the size of the array in a parameter.
It is possible to specify the dimension of the array using references, but the caller will be bounded to the size on the arguments
void function(int (&arr)[10]); // only arrays of size 10 accepted
Arrays of arrays
Scope
- Global
- Class
- Namespace
- Block
Delimited by curly braces
Local variables (automatic objects)
Static variables
By value vs By reference
Parameter initialization works the same way as variable initialization.
When an argument value is copied, the parameter and the argument are independent objects.
Changes made to the variable have no effect on the initializer.
Using references to avoid copies
Reference
TODO
Essentially an alias to a variable.
You can return multiple values from a function.
Pointer
// Initialize to null
int* intPtr = 0;
float* floatPtr = 0;
int intVar = 50;
float floatVar = 3.1415f;
// Initialize pointers to an address
int* intPtr = &intVar;
float* floatPtr = &floatVar;
Derreferencing
Constant pointers
float* const constFloatPtr; // the pointer is constant, the variable pointed to CAN change
float const* constFloat0; // the pointer is NOT constant, the variable pointed CAN't change
const float* constFloat1;
const float* const constFloatConstFloatPtr; // pointer and the variable pointed CAN'T change
TODO
const parameters
void f1(const int i){ ... }
void f1(int i) { ... } // WRONG!
Can't overload a function: parameter list are not sufficiently different.
Refer to example in p 283 & 283
The complete main function
int argc, char ** argv
Use command line arguments
Defined in cstdlib when exiting main (not std:: scope needed)
EXIT_FAILURE
EXIT_SUCCESS
Overloaded functions
Error if they only differ in their return types
Default arguments
Inline
Avoid function call overhead.
Is a request to the compiler, it might be ignored
Macros
assert only works if NDEBUG is not defined
To turn off asserts, NDEBUG must be defined.
FILE
LINE
TIME
DATE
Dynamic Memory
- Allocation
- Deallocation
- Memory Leaks
QUIZ
- dynamic allocation for variable, array, deallocate dynamic variable
- runtime, compile time memory allocation
- a function exists in memory and therefore has a memory address?
- About references, can we initialize a reference to NULL?
- about pointers, passing an array to a function parameter
- Pointers and symbols & # ++ -1
- comparing vectors and dynamicly allocated arrays
- example of memeory address and variable
- example pointer arithmetic
- About functions returning more than one result using references or pointers