C Foundation

What is C? C Compiler Installation C Extensions C Compiler C Interpreter C Program Structure

C Basics

C Keywords C Data Types C Identifiers C Variables C Constant C Escape Sequences C Constant and Volatile C Typecast

Operators

What is Operator C Comma Operator C Arithmetic Operators C Relational Operators C Logical Operators C Bitwise Operators C Conditional Operators C : : Operator C Operator Priority

Basic IO's

Basic IO's C Formatted Functions C Unformatted Functions C Common Functions

Control Statements

What is Control Statement C if Statement C if else Statement C Nested if Statement C Else if Statement C Break Statement C Continue Statement C Switch Statement C Goto Statement

Looping

What is Control Loop C for Loop C Nested for Loop C while Loop C Nested while Loop C do while Loop C Nested do while loop

Functions

What is Function C User Defined Functions C Recursion C Passing Parameters

Scope

Scope C Local Scope C Global Scope

Storage Classes

What is Storage Class C Auto C Extern C Static C Register

Array

What is Array C One Dimensional Array C Two Dimensional Array C Multi Dimensional Array C Arrays Of Strings

String

What is String C String Functions

Pointer

What is Pointer C Pointers Arithmetic C Pointer to Pointer C Pointers and Arrays C Pointers and Strings C Pointer to Functions Void Pointers Null Pointers C Null and Void Pointer

Structure

What is Structure C Struct within Struct C Array within Structure C Pointer to Structure C Structure and Function C Enum C Bitfield Structure C Type def

Union

What is Union

Files

What is File C read a file C write a file C File Handling C Error Handling C Low Level Disk I/O C Other file functions

Memory Allocation

What is Memory Allocation C Malloc() C Calloc() C Free() C Realloc() C Coreleft()

C Reference

All ASCII Code Basic C Questions

C Interview

C Interview Sets All Star Patterns All Number Patterns All Alphabet Patterns All Series Patterns
The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Pointer Arithmetic

C programming allow programmers just like you to do arithmetic operations using pointers. So, programmers can perform any arithmetic operations using pointer variables. Performing arithmetic operations using pointer variables is said to be arithmetic pointer.

pointer arithmetic in c

C Program - Pointer Arithmetic

pointers-arithmetic.c
#include <stdio.h>
int main()
{
int a = 10, b = 6;
int *ptr, *ptr1;
ptr = &a;
ptr1 = &b;
printf("sum of two pointers : %d \n", *ptr+b);
printf("Subtraction of two pointers : %d \n", a-*ptr1);
return 0;
}
  • sum of two pointers : 16
  • subtraction of two pointers : 4

Note:

In the above program, two integer pointers ptr and ptr1 are declared and initialized by the address of a and b respectively. Then, *ptr + b and a - *ptr1 is used to perform Addition and Subtraction using pointers.

Incrementing Pointer variable

Suppose if your interviewer ask you to display the value stored in an array without using any index value. Then Pointer will help you to get there by using increment operator.

Program - Incrementing Pointer variable

increment-pointer.c
#include <stdio.h>
int main()
{
int arr[3] = {10, 11, 12};
int *ptr, i;
ptr = arr;
for(i = 0; i < 3; i++)
{
printf("%d\t", *ptr);
ptr++;
}
return 0;
}
  • 10      11      12

Note:

Here pointer variable ptr is initialized with the address of first element in an array arr. We know that array stores the elements consecutively in it. Then its very simple for a pointer to point to the next memory location using increment operator.

Decrementing Pointer Variable

Suppose if your interviewer ask you to display the value stored in an array in reverse order by providing you with the address of last value in an array and restricting not to using index value iteratively. Then Pointer will help you to get there by using decrement operator.

Program - Decrementing Pointer variable

decrement-pointer.c
#include <stdio.h>
int main()
{
int a[3] = {10, 11, 12};
int *ptr, i;
ptr = &a[2];
for(i = 3; i > 0; i--)
{
printf("%d\t", *ptr);
ptr--;
}
return 0;
}
  • 12      11      10

Note:

Here pointer variable ptr is initialized with the address of last element in an array arr. We know that array stores the elements consecutively in it. Then its very simple for a pointer to point to the previous memory location using increment operator.

Comparing Pointer Variable

Pointer variable can be compared either with other pointer variable or with normal variable. Relational operators will get you there for comparison operation. Let us using greater than operator to compare two pointer variables.

Program - Comparing Pointer variable

comapring-pointer.c
#include <stdio.h>
int main()
{
int a = 10, b = 6;
int *ptr, *ptr1;
ptr = &a;
ptr1 = &b;
if(*ptr > *ptr1)
printf("value stored in a is greater than b ");
else
printf("value stored in b is greater than a ");
return 0;
}
  • value stored in a is greater than b

Note:

Here *ptr and *ptr1 will fetch the value from the address stored in it and greater than ( > ) operator helps us to find greatest among two pointer variables.

Did You Know?

Pointer variable can be used for all sort of operations like addition, subtraction, division, passing value and even more as we do with normal variable. But pointer variable can play with address of memory which cannot be done using normal variable is the only difference.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report

We to update you