Constant Pointer to Non-Constant Data Constant Pointer to Non-Constant Data sizeof Operator - sizeof operator
- Returns size of operand in bytes
- For arrays, sizeof returns
( size of 1 element ) * ( number of elements ) - If sizeof( int ) returns 4 then int myArray[ 10 ]; cout << sizeof( myArray );
will print 40 - Can be used with
- Variable names
- Type names
- Constant values
The getsize function return number of bytes used to store array address
Pointer Expressions and Pointer Arithmetic - Pointer assignment
- Pointer can be assigned to another pointer if both are of same type
- If not same type, cast operator must be used
- Exception
- Pointer to void (type void *)
- Generic pointer, represents any type
- Casting is needed to convert void * to any other type
- void pointers cannot be dereferenced
Relationship Between Pointers and Arrays Arrays and pointers are closely related - Array name is like constant pointer
- Pointers can do array subscripting operations
Accessing array elements with pointers - Assume declarations:
int b[ 5 ]; int *bPtr; bPtr = b; - Element b[ n ] can be accessed by *(bPtr + n )
- Called pointer/offset notation
- Addresses
- &b[ 3 ] is same as bPtr + 3
- Array name can be treated as pointer
- b[ 3 ] is same as *( b + 3 )
- Pointers can be subscripted (pointer/subscript notation)
- bPtr[ 3 ] is same as b[ 3 ]
Arrays of Pointers Arrays can contain pointers Commonly used to store array of strings (string array) - Array does not store strings, only pointers to strings
- Example
- const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
- Each element of suit points to a char * (string)
- suit array has fixed size (4), but strings can be of any size
Function Pointers - C++ allows operations with pointers to functions.
- We know that every function code along with its variables is allocated some space in the memory, Thus every function has an address.
- Function pointers are pointer variables that points to the address of a function.
- Similar to other pointer variables , function pointers can be declared, assigned values and used to access the functions they point to.
The syntax of declaring a function pointer is Return-type (*function_pointer_name) (argument_list); e.g. Int (*func) (int a, float b) // function pointer Int * func (int a, float b) // function returning pointer to int Initializing a Function Pointer - A function pointer must be initialized prior to use.
- If we have declared a pointer to the function , then that pointer can be assigned address of correct function just by using its name.
- Like in the case of an array, a function name is changed into an address when it’s used in an expression.
- Its optional to use the address operator (&) in front of the function name.
e.g. fp //function pointer int add(int a, int b) fp=add(int,int) // initialize the function pointer fp with address of add Comparing Function Pointers Comparison operators such as == and != can be used the same way as usual. Consider the code given below which checks if fp actually contains the address of the function. Passing Function Pointer as an argument to a function
Do'stlaringiz bilan baham: |