Todayโs Outline
- Recap: Variable and Memory
- Pointer and its operations
- Pass by Pointer
- Array and Pointer
Recap: Variable and Memory
- A variable is normally used to store data in the main memory
- A variable has 5 attributes
- Value
- Type
- Name
- Address
- Scope
- The address of a variable is usually in hexadecimal
- 0x00023AF0 for 32-bit computers
- 0x00006AF8072CBEFF for 64-bit computers
Pointer and its operations
Definition of Pointer
- A pointer is a
variable
that stores the memory address of another variable
- When a pointer stores the address of a variable, we say
the pointer is pointing to the variable
- A pointer, like a normal variable, has a type. The
pointer type
is determined by the type of the variable it points to
Basic Pointer Operators: & and *
& address operator
: get address of a variable
*
is used in 2 ways
* in declaration
: it indicates a pointer type
- e.g.
int *p
is a pointer that points to an int variable
* in other statements
: it is a dereference operator
Common Pointer Operations
Common Errors
Pass by Pointer
Pass-by-Reference vs Pass-by-Pointer
Pass-by-Pointer vs Pass-by-Reference
Example: Swapping Value
Array and Pointer
- Supposed to print the address of
'H'
but prints 'Hello World'
because it is a string
Array Variable is NOT a Pointer
Arrays as Parameters