
*Pointers , […]Arrays & Recursion
Do you get it now?
It has been two weeks since, I believe. Boggling my mind to come to terms with everything. And debugging. deep heavy sigh.
Ever since I knew about rubber duck debugging ,— taking a rubber duck and explaining your whole code to it. All of it . — I feel like all my errors gaslight me . Like it’s not me, it’s you. An endless one-way conversation with my laptop.
what the [bleep]? — -Nothing
why won’t you [bleep] worrrrk!!! — silence
agh [bleep] this btw
oh shit.
Gruesome. I know. Let’s get on with it.
*Pointers
Simply put, there are those people in life who always knows a guy. No, they do not have what you want, but they know someone who has. That guy is a *pointer. What you want now, is the pointed.
- These are used to store addresses of the pointed.
- They point you to where the value is. - - Pointers are declared by denoting an asterisk just before the variable:
(ways of declaring a pointer)int *p; --- pointer to an integerchar *c -- pointer to a characterint* p1, p2; -- we have declared a pointer p1 and a normal variable p2.
- They hold the addresses with the (&) sign
- Assigning Pointers with value.
Here we initiate pointer pc and var c. We assign it a value 5. Then we say our pointer will point(hold address) our pointed.
int* pc(pointer), c(pointed);c = 5;
pc = &c;
- Get value of the pointed.
The address ofc
is assigned to the pc pointer. To get the value stored in that address, we used *pc.
printf("%d", *pc); //output 5.do not use *pc = &c; -- (just don't)
- Change value of the pointed.
After initialization, we append our values.
We then store the address of our pointed in our *pointer.
We then change the value of our pointed and try to print out the value, hence.
int* pc, c;c = 5;
pc = &c;
c = 1;printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
- There are double **pointers. They point to the pointer of the pointed. (These are the guys who knows a guy who knows a guy.)
- More information on pointers can be found here and here.
arrays [value]
- The mother of storage. It can be used to store alot of values in a single variable. They are declared as :
dataType varName[no_of_elements]int t [5]; //Here we have initialized an array of 5 integers.
- Initialization
These are the possible ways to initialize our variables.
int t[5] = {5, 68, 1000, 3, 467};
int t[] = {5, 68, 1000, 3, 467};
- Accessing arrays
Arrays always start with a zero when counting it’s values.
To access the value1000
in our array , we count from 0 which space it is in.
int t[2]; /// output 1000
int t[4]; ///output 467
- Changing values.
To change the value , we access the array we want and assign a new value.
int [0] = 1;
int [0]; //output will be 1 from 5
- Multidimensonal Arrays.
These are arrays that hold other arrays.int x [y][z]
||int a [b][c][d]
- A times table is an example of two dimensional arr. More information ← — there.
Recursion.
Very recursive concept. You can get it then you don’t. You repeat the process till you get it. — Get it? No?Okay.
Recursion is a function that calls itself. The explanation on why it does this is easy.
- It breaks down the problem to smaller problems(solutions).
- It solves the smallest problem and repeats itself back up till it solves the biggest problem.
Case Example: You are 15 min walk away from home.
function goHome(){
first ,if you are at home -- we stop walking //(base case) if not, we take a step towards home //method goHome(); //function calling itself
}
This is a continous process of walking till we get home. By taking small steps , we solve the function of going home.
- We see the structure of a recursive function.
1. Base case — this is the end-point of our case.
It checks if we have fullfilled our whatever we wanted.
It prevents the function from forever executing.
2. Method — This is the body of our function, where we write how we are to solve this small task.
3. Function calling — The function calls itself. This loops it back to the base case and then ….repeats itself. See, recursive.
Now, do you get it?
Yes? Good .
No? Let’s try this again.
- Major life examples are factorial! and fibonacci(next num is the sum of the previous two numbers).
It always starts with 0, 1. So, a series (9) of fibonacci is :
0, 1, 1, 2, 3, 5, 8, 13, 21
There is much I can write to a point, but I hope we have understood this.
Commit count: 420.