Reflection: Pointer
I, Summary
II, Applied to solve problems
1. void input(int*n, int*m); – input and check n>0, m>0
III, Some of my thoughts
Some situations where pointers can be used are: to return more than one value from a function, to pass arrays and strings more conveniently from one function to another, to manipulate arrays easily by moving pointers to them instead of moving the arrays itself, and to allocate memory and access it (Direct Memory Allocation). With pointer, I can use this address an easy way. To understand pointers, it helps to compare them to normal variables.
What is pointer? Pointers are the container/variable who holds address of other variables. C uses pointers in three different ways:
- C uses pointers to create dynamic data structures -- data structures built up from blocks of memory allocated from the heap at run-time.
- C uses pointers to handle variable parameters passed to functions.
- Pointers in C provide an alternative way to access information stored in arrays. Pointer techniques are especially valuable when you work with strings. There is an intimate link between arrays and pointers in C.
The declaration of pointers follows this format:
type * name;
II, Applied to solve problems
1. void input(int*n, int*m); – input and check n>0, m>0
2. int fun2(int n, int m); Function returns n! + mn if n > 0 and m > 0, otherwise return 0
3. double fun6(int n); Function returns value calculated by formula
4. int fun7(int n, int m);
Function returns the highest prime number < nm
5. int fun8(int n, int m);
Function returns the highest Fibonacci number < nm
Propose myself 3 examples using pointer:
Example 1: Swap 2 numbers
Example 2: Sort 3 numbers following increasing order.
Example 3: Return the character before c:
Some situations where pointers can be used are: to return more than one value from a function, to pass arrays and strings more conveniently from one function to another, to manipulate arrays easily by moving pointers to them instead of moving the arrays itself, and to allocate memory and access it (Direct Memory Allocation). With pointer, I can use this address an easy way. To understand pointers, it helps to compare them to normal variables.
Comments
Post a Comment