A pointer is a variable which stores the address of another variable.
The address of operator is used to obtain the address of a given variable.
- Format specifier for printing pointer address is '%u'
- The value at adress operator (*)
- The value at address operator is used to obtain the value present at a given memory address. It is denoted by *
i = 72 address of i = 87994 j = 87994 address of j = 87998 j is a pointer j points to i Address of operator-- &i => 87994 &j => 87998 value at address operator- *(&i) = 72 *(&j) = 87994
Syntax--
int *j; // declare a variable j of type //int-pointer j = &i // store address of i in j just like this------------------- int *ch_ptr //pointer to integer char *ch_ptr //pointer to character float *ch_ptr // pointer to float
we can have a variable which can store the address of j .
int **k k = &j;
Based on the way we pass arguments to the function, function calls are of two types:-
- Call by value
- Call by reference
The value of arguments are passed to the function. we can't change variables of main function with another function.
int c = sum(3,4); // assume x=3 and y=4
The address of the variables is passed to the function as arguments.
- Write a program to print the address of a vairable.Use this address to get the value of this variable.
- Write a program having a variable i. Pass this variable to a function and print its address. Are those addresses same? Why?
- Write a program to change the value of a variable to ten times of its current value. Write a function and pass the value by reference.
- Write a program using function which calculate 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 "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.