Programming in C: Unit III (b): Pointers

Pointer Expressions and Pointer Arithmetic

with Example C Programs

Like other variables, pointer variables can also be used in expressions. For example, if ptr1 and ptr2 are pointers, then the following statements are valid.

POINTER EXPRESSIONS AND POINTER ARITHMETIC

Like other variables, pointer variables can also be used in expressions. For example, if ptr1 and ptr2 are pointers, then the following statements are valid.

int num1=2, num2= 3, sum=0, mul=0, div=1;

int *ptrl, *ptr2;

ptrl = &num1;

ptr2 = &num2;

sum = *ptrl + *ptr2;

mul = sum* *ptrl;

*ptr2 +=1;

div = 9 + *ptr1/*ptr2 - 30;

In C, the programmer may add or subtract integers from pointers. We can also subtract one pointer from the other. We can also use short hand operators with other pointer variables as we use with other variables.

Programming Tip: A pointer variable that is declared but not initialized contains garbage value. Hence, a pointer variable must not be used before it is assigned any variable's address.

C also allows to compare pointers by using using relational operators in the expressions. For example, p1 > p2, p1 == p2, and p1! = p2 are all valid in C.

When using pointers, unary increment (++) and decrement (--) operators have greater precedence than the dereference operator (*). Both these operators have a special behaviour when used as suffix. In that case the expression is evaluated with the value it had before being increased. Therefore, the expression

*ptr++

is equivalent to * (ptr++) as ++ has greater operator precedence than *. Therefore, the expression will increase the value of ptr so that it now points to the next memory location. This means the statement *ptr++ does not perform the intended task. Therefore, to increment the value of the variable whose address is stored in ptr, you should write

(*ptr) ++

Now, let us consider another C statement

int num1=2, num2=3;

int *p = &numl, *q=&num2;

*p++ = *q++;

What will *p++ = *q++ do? Because ++ has a higher precedence than *, both p and q are increased, but because the increment operators (++) are used as postfix and not prefix, the value assigned to *p is *q before both p and q are increased. Then both are increased. So the statement is equivalent to writing:

Programming Tip: It is an error to subtract two pointer variables.

*p = *q;

++p; ++q;

Let us now summarize the rules for pointer operations:

• A pointer variable can be assigned the address of another variable (of the same type).

• A pointer variable can be assigned the value of another pointer variable (of the same type). - vib

• A pointer variable can be initialized with a NULL (or 190) value.

• Prefix or postfix increment and decrement operators can be applied on a pointer variable.

• An integer value can be added or subtracted from a pointer variable.

• A pointer variable can be compared with another pointer variable of the same type using relational operators.

• A pointer variable cannot be multiplied by a constant.

• A pointer variable cannot be added to another pointer variable.

2. Write a program to print Hello World, using pointers.

#include <stdio.h>

int main()

{

char *ch = "Hello World";

printf("%s", ch);

return 0;

}

Output

Hello World

3. Write a program to add two floating point numbers. The result should contain only two digits after the decimal.

#include <stdio.h>

int main()

{

float num1, num2, sum = 0.0;

float *pnum1 = &num1, *pnum2 = &num2, *psum = &sum;

printf("\n Enter the two numbers: ");

scanf("%f %f", pnuml, pnum2);

// pnum1 &numl;

*psum *pnuml + *pnum2;

printf("\n %f + %f = %.2f", *pnum1, *pnum2, *psum);

return 0;

}

Output

Enter the two numbers: 2.5 3.4

2.5 + 3.4 = 5.90

4. Write a program to calculate area of a circle.

#include <stdio.h>

#include <conio.h>

int main()

{

double radius, area = 0.0;

double *pradius = &radius, * parea = & area;

printf("\n Enter the radius of the circle: ");

scanf("%1f", pradius);

*parea 3.14 * (*pradius) * (*pradius);

printf("\n The area of the circle with radius %.21f = %.21f", *pradius, *parea);

return 0;

}

Output

Enter the radius of the circle: 7

The area of the circle with radius 7.00 = 153.83

5. Write a program to convert a floating point number into an integer.

#include <stdio.h>

int main()

{

float fnum, *pfnum =  &fnum;

int num, *pnum = &num;

printf("\n Enter the floating point no.: ");

scanf("%f", &fnum);

*pnum = (int) *pfnum;

printf("\n The integer equivalent of %.2f = %d", *pfnum, *pnum);

return 0;

}

Output

Enter the floating point no.: 3.4

The integer equivalent of 3.40 = 3

6. Write a program to find the biggest of three numbers.

#include <stdio.h>

int main()

{

int numl, num2, num3;

int *pnum1 &num1, *pnum2= &num2, *pnum3= &num3;

printf("\n Enter the first number: ");

scanf("%d", pnum1);

printf("\n Enter the second number: ");

scanf("%d", pnum2);

printf("\n Enter the third number: ");

scanf("%d", pnum3);

if (*pnuml > *pnum2 && *pnum1 > *pnum3)

printf("\n%d is the largest number", *pnum1);

if (*pnum2 > *pnum1 && *pnum2 > *pnum3)

printf("\n%d is the largest number", *pnum2);

else

printf("\n%d is the largest number", *pnum3);

return 0;

}

Output

Enter the first number: 5

Enter the second number: 7

Enter the third number: 3

7 is the largest number

7. Write a program to print a character. Also print its ASCII value and rewrite the character in upper case.

#include <stdio.h>

#include <conio.h>

int main()

{

int ch, *pch = &ch;

clrscr();

printf("\n Enter the character: ");

scanf("%c", &ch);

printf("\n The char entered is: %c", *pch);

printf("\n ASCII value of the char is: %d", *pch);

printf("\n The char in upper case is: %c", *pch - 32);

getch();

return 0;

}

Output

Enter the character: z

The char entered is: z

ASCII value of the char is: 122

The char in upper case is: Z

8. Write a program which takes an input from the user and then checks whether it is a number or a character. If it is a character, determine whether it is in upper case or lower case.

#include <stdio.h>

#include <conio.h>

int main()

{

char ch, *pch = &ch;

clrscr();

printf("\n Enter any character: ");

scanf("%c", pch);

if (*pch >='A' && *pch<='Z' )

printf("\n Upper case char was entered");

else if (*pch >='a' && *pch<='z')

printf("\n Lower case char was entered");

else (*pch>='0' && *pch<='9')

printf("\n You entered a number");

getch();

return 0;

}

Output

Enter any character: 7

You entered a number

9. Write a program using pointer variables to read a character until * is entered. If the character is in upper case, print it in lower case and vice versa. Also count the number of upper and lower case characters entered.

#include <stdio.h>

#include <conio.h>

int main()

{

char ch, *pch = &ch;

int upper = 0, lower = 0;

clrscr();

printf("\n Enter the character: ");

scanf("%c", pch);

while (*pch != '*')

{

if (*pch >= 'A' && *pch <= 'Z')

{

*pch += 32;

upper++;

}

if (*pch >= 'a' && *pch <= 'z')

{

*pch = = 32;

lower++;

}

printf("%c", *pch);

printf("\n Enter the character: ");

scanf("%c", pch);

printf("\n Total number of upper case characters = %d", upper);

printf("\n Total number of lower case characters= %d", lower);

getch();

return 0 ;

}

Output

Enter the character: A

a

Enter the character: b

B

Enter the character: c

C

Enter the character: *

Total number of upper case characters = 1

Total number of lower case characters = 2

10. Write a program to test whether a number is positive, negative, or equal to zero.

#include <stdio.h>

int main()

{

int num, *pnum = &num;

printf("\n Enter any number: ");

scanf("%d", pnum);

if (*pnum>0)

printf("\n The number is positive");

else

{

if (*pnum<0)

printf("\n The number is negative");

else

printf("\n The number is equal to zero");

}

return 0;

}

Output

Enter any number: -1

The number is negative

11. Write a program to display the sum and average of numbers from m to n.

#include <stdio.h>

int main()

{

int num, *pnum = &num, range;

int m, *pm = &m;

int n, *pn = &n;

int sum = 0, *psum = &sum;

float avg, *pavg = &avg;

printf("\n Enter the starting and ending limit of the numbers to be summed: "); scanf("%d %d", pm, pn);

range = n - m;

while (*pm <= *pn)

{

*psum = *psum + *pm;

*pm = *pm + 1;

}

printf("\n Sum of numbers = %d, *psum);

*pavg = (float) *psum / range;

printf("\n Average of numbers = %.2f", *pavg);

return 0;

}

Output

Enter the starting and ending limit of the numbers to be summed: 0 10

Sum of numbers = 55

Average of numbers 5.50

12. Write a program to print all even numbers from m to n.

#include <stdio.h>

#include <conio.h>

int main()

{

int m, *pm = &m;

int n, *pn = &n;

printf("\n Enter the starting and ending limit of the numbers: ");

scanf("%d %d", pm, pn);

while (*pm <= *pn)

{

if (*pm %2 = = 0)

printf("\n %d is even", *pm);

(*pm) ++;

}

return 0;

}

Output

Enter the starting and ending limit of the numbers: 0 10

0 is even

2 is even

4 is even

6 is even

8 is even

10 is even

13. Write a program to read numbers until -1 is entered. Also display whether the number is prime or composite.

#include <stdio.h>

int main()

{

int num, *pnum = &num;

int i, flag = 0;

printf("\n ***** ENTER -1 TO EXIT *****);

printf("\n Enter any number: ");

scanf("%d", pnum);

while(*pnum != -1)

{

if (*pnum = = 1)

printf("\n %d is neither prime nor composite", *pnum);

else if (*pnum = = 2)

printf("\n %d is prime", *pnum);

else

{

for (i=2; i<*pnum/2; i++)

{

if (*pnum/i = = 0)

flag =1;

}

if (flag = = 0)

printf("\n %d is prime", *pnum);

else

printf("\n %d is composite", *pnum);

}

printf("\n Enter any number: ");

scanf("%d", pnum);

}

return 0;

}

Output

***** ENTER -1 TO EXIT *****

Enter any number: 3

3 is prime

Enter any number: 1

1 is neither prime nor composite

Enter any number: -1

Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Pointer Expressions and Pointer Arithmetic