Skip to content

Latest commit

 

History

History

chapter06

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Chapter 6- Pointers

A pointer is a variable which stores the address of another variable.

The address of (&) operator

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

Declaring a pointer

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

Pointer to a Pointer

we can have a variable which can store the address of j .

int **k
k = &j;

Types of Function Calls

Based on the way we pass arguments to the function, function calls are of two types:-

  1. Call by value
  2. Call by reference

Call by value-

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

Call by reference

The address of the variables is passed to the function as arguments.

Exercises-

  1. Write a program to print the address of a vairable.Use this address to get the value of this variable.
  2. Write a program having a variable i. Pass this variable to a function and print its address. Are those addresses same? Why?
  3. 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.
  4. 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().
  5. write a program to print the value of a variable i by using "pointer to pointer"type of variable.
  6. Try problem 3 using call by value and verify that it doesn't change the value of the said variable.