Chapter 6 - Pointers
A pointer is a variable that stores the address of another variable.

j is a pointer
j points to i.
The "address of" (&) operator
The address of operator is used to obtain the address of a given variable
If you refer to the diagrams above
Format specifier for printing pointer address is ‘%u’
The "value of address" operator (*)
The value at address or * operator is used to obtain the value present at a given memory address. It is denoted by *
How to declare a pointer?
A pointer is declared using the following syntax,
Just like pointer type integer, we also have pointers to char, float, etc.
Although it's a good practice to use meaningful variable names, we should be very careful while reading & working on programs from fellow programmers
A Program to demonstrate Pointers:
Output:
This program sums it all. If you understand it, you have got the idea of pointers.
Pointers to a pointer:
Just like j is pointing to i or storing the address of i, we can have another variable, k which can store the address of j. What will be the type of k?

We can even go further one level and create a variable l of type int*** to store the address of k. We mostly use int* and int** sometimes in real-world programs.
Types of function calls
Based on the way we pass arguments to the function, function calls are of two types.
- Call by value -> sending the values of arguments.
- Call by reference -> sending the address of arguments
Call by value:
Here the values of the arguments are passed to the function. Consider this example:
If sum is defined as sum(int a, int b), the values 3 and 4 are copied to a and b. Now even if we change a and b, nothing happens to the variables x and y.
This is call by value.
In C, we usually make a call by value.
Call by reference:
Here the address of the variable is passed to the function as arguments.
Now since the addresses are passed to the function, the function can now modify the value of a variable in calling function using * and & operators. Example:
This function is capable of swapping the values passed to it. If a=3 and b=4 before a call to swap(a,b), a=4 and b=3 after calling swap.
Chapter 6 - Practice Set
- Write a program to print the address of a variable. Use this address to get the value of this variable.
- Write a program having a variable i. Print the address of i. Pass this variable to a function and print its address. Are these addresses same? Why?
- Write a program to change the value of a variable to ten times its current value. Write a function and pass the value by reference.
- Write a program using a function that calculates the sum and average of two numbers. Use pointers and print the values of sum and average in main().
- Write a program to print the value of a variable i by using the "pointer to pointer" type of variable.
- Try problem 3 using call by value and verify that it doesn’t change the value of the said variable.