Pointer
Firstly, let’s review how to print the hexadecimal(16 base)
Remember: Every time the program is run, the address of data will likely be different.
When declare pointers for different types of variables:
Notice : The type of the right-hand side must match the type of the left-hand side.
Dereferencing:
It is possible to take a memory address and get the data that this address points to
Dereferencing is also known as indirection... as we can access data in memory indirectly, via pointer
When run it, compile will crash because the null pointer cannot be found.
The Dangling Pointer
A dangling pointer is a pointer that once stored a valid address, but the address has since become invalid
Remember, local variables, and their contents, no longer valid when a function return.
Pass by Reference
Pointers can be used as parameters to functions
Allowing the calling function to pass in the address of a piece of memory
This allows the function to refer to the caller’s memory
Swap data:
In this sample, a and b are only accessible in the bad_swap function, When the function returns, a and b disappear
Also, the call to bad_swapdoes not change the data stored in x and y inside the main function
The swap function is using x and y to calculate actually….(not the a and b)
Returning Multiple Things from Functions
A Pointer to a Structure
Declaring a pointer named: p_lecturer
The ->Operator
The ->operator allows access to the members of a structure via pointer
Because the for the .operator, cannot use the pointer directly.
Passing Structures to function via pointer
Arrays and Pointers
As the name of an array is actually a pointer to the first element in the array.
Passing Arrays as Parameters
(Arrays are passed to functions by reference)
The callee can alter the caller’s data
Passing Arrays using Pointers
Arrays can be passed into functions via pointer.
The name of the array is actually a pointer to the first element