C Programming

# Pointers in C

Pointer is a very important concept in C language because pointer gives us the concept of address in memory. Everything that is not accessible by the others, pointer can access this very easily with the help of address.

Here we will discuss the basic concept of pointer.

## Objective:

The main objective of the pointer is mainly:

## 1. Extended concept of pointer:

X p q r    1000 2000 3000 4000

## Example:

 123456789101112131415 #include Void main () {     Int x = 5 , *p , **q , ***r ;     p = &x ;     q = &p ;     r = &q ;     **q = 7 ;     ***r = 7 ; }

## Explanation:

Here p is a pointer. From the line p = &x, it is understand that p is a pointer holding the address of x .

**q is also a one kind of pointer whose level of indirection is 2 and r is also a pointer whose level of indirection is 3. How many * are there that is level of that pointer?

The rule is, the level of pointer is such that it holds the address of another variable whose level of indirection is exactly one less of that pointer.

For this q holds the address of P. P holds the address of x.

The procedure of read the line int x = 5,*P, **q , ***r ;

x is an int. p is a pointer to an int. q is a pointer to apointer to an int. How many ** are there? we say exactly same no of word pointer. Just like **r, it means r is a pointer, to a pointer, to a pointer to an int.

If we put some value with the help of q we write

 1 *q =p , *r = q , *p = x;

If we write a pointer or * before a pointer variable, it turns into a such variable that points to the variable mean, p block.

## 2. pointers Arithmetic:

We cannot add multiply or divide in two addresses (subtraction is possible).

We cannot multiply an integer to an address. Similarly, we cannot divide an address with an integer value.

## Programming Example 1:

 123456789101112131415161718192021 #include void main () { int a , b ; int *p , *q ; p = &a ; q = &b ; printf ( " %d ", p + q ) ; printf ( " %d ", p * q ) ; printf ( " %d ", p / q ) ; }

## Output: ## Explanation:

In the above-mentioned program, we are trying to add, multiply, and divide the two addresses by writing some statements.

 12345 printf ( " %d ", p + q ) ; printf ( " %d ", p * q ) ; printf ( " %d ", p / q ) ;

But it is not possible as you can see from the output. Pointer never gives permission to add, multiply, and divide some address.

Pointer can allow some mathematical calculation. They are mentioned below:

 1234567 P = 1000 P + 1 = 1002 P + 4 = 1008 P – 1 = 998

P is integer type pointer 1000 that is based on the address of a. “a” variable has two bytes. The address of 1st byte is 1001 & the address of 2nd byte is 1002.  If we add 1 to a pointer, it makes the address of the next block or next variable instead of address of next byte.

 12345 Pointer+ n = pointer + size of ( type of pointer ) * n = 1000   +   2 * 1 = 1002

We can subtract two addresses of same type. Pointer permits it.

## Programming Example 2:

 1234567891011121314 #include void main () {     int a , b ;     int *p , *q ;     p = &a ;     q = &b ;     printf ( " %d ", p - q ) ; }

## Output: ## Explanation:

Here, we are trying to subtract two addresses. Fortunately, it can support by the pointer. It is not literally subtracted. It is made up of a block depending on its data type.

## Formula

 1 Pointer 1 – pointer 2 = ( Literal subtraction ) / size of ( type of pointer )

If it is char type, divided by 4.

If it is float type, divided by 4.

If it is int type, divided by 2.

## Programming Example 3:

 12345678910111213141516171819202122232425262728 #include void swap( int * , int * ) ; int main() {     int a , b ;     printf ( " enter two number " ) ;     scanf("%d%d",&a,&b);     swap( &a, &b ) ;     printf ( " a = %d b = %d ",a ,b) ; } void swap( int *x , int *y ) {     int t ;     t = *x ;     *x = *y ;     *y = t ; }

## Output: ## Explanation:

Here, a pointer can pass through a function. This is called call by references. If we want to swap two integers by passing the values of two integers through a function, it is not possible.

We must pass the address of the variable t = &x. If we add * to a pointer variable, then *x turned into such variable that hold the value of the variable, which it points the p variable. It means t = *x, means, t holds the value of an indirectly.

## Call by references:

Call by references is same as call by address. When Formal Arguments are pointer variables, it is call by references.

Reference means address. Call by Reference means, when we call a function, we pass the addresses of the variables, it is called call of reference.

An important question may arise related to address or why we use address of ( & ) in scanf ()?

Scanf ( ) is a predefined function in C language. main () is also a function in C language. We use scanf ( ) inside main (). So, if we declare two variables inside main () function, we access this variable in scanf ( ). We can’t use variables of a function to another function. So, scanf ( ) function wants to put some value to a variable and b variable, then it has to know the address of a and b.

For this address of (&) is used in scanf ().

## Conclusion:

We can know all the basic concept of pointer. From this discussion we have come to this conclusion that without pointer we cannot visualise the memory management in C language. Address controls the entire memory management scheme. For this we must know the concept of pointer. 