C Programming language

C Standard Library Function

Day 1: Introduction and brief history of C Programming language

Day 1: Advantages and Disadvantages of C, C Keywords, Data type modifiers in C

Day 1: Data types in C Programming language

Day 1: Secondary data types, Primitive and Non-primitive data types

Day 1: C Variables, C Constant, Format Specifiers in C

Day 2: Write first C program

Day 2: Flow of C program with example, main(), printf(), scanf()

Day 2: Operaters in C Programming language, Arithmetic operators

Day 2: Relational operators and Logical Operators in C Programming language

Day 2: Assignment, Increments and Decrement Operators in C Programming language

Day 3: Conditional statement: if else statement in C Programming language

Day 3: Conditional statement: switch statement in C Programming language

Day 3: Jump statements: return statement in C Programming language

Day 3: Jump statements: go to statement in C Programming language

Day 3: Jump statements: break statement in C Programming language

Day 3: Jump statements: continue statement in C Programming language

Day 4: Loops OR Iteration statement in C Programming language: for Loop

Day 4: Loops OR Iteration statement in C Programming language: while Loop

Day 4: Loops OR Iteration statement in C Programming language: do while Loop

Day 5: Array in C Programming language

Day 5: Access elements of Array in C Programming language

Day 5: One dimensional Array representation in memory using C Programming language

Day 5: Two dimensional Array representation in memory using C Programming language

Day 5: Multidimensional Array in C Programming language

Day 6: Function in C Programming language

Day 6: Definition, Declaration and Calling a Function in C Programming language

Day 6: Passing array to a function in C Programming language

Day 6: Calling Function in C Programming language : Call by value

Day 6: Calling Function in C Programming language : Call by reference

Day 6: Recursive Function in C Programming language

Day 6: Adding function to the library in C Programming language

Day 7: Pointer in C Programming language, How to use Pointer, Pointer declaration

Day 7: NULL Pointers in C Programming language

Day 7: Array of Pointers in C Programming language

Day 7: Pointer arithmetic in C Programming language

Day 7: Pointer to Pointer in C Programming language

Day 7: Pointer to Function in C Programming language: Passing pointers to functions

Day 7: Pointer to Function in C Programming language: Return pointer from functions

Day 8: Strings in C Programming language, Declaring String in C Programming language

Day 8: String functions in C Programming language

Introduction of Structure

Accessing the members of Structure

Structure With typedef Keyword and Use of sizeof function

Example of Structure

Dynamic memory allocation in C: Introduction

adplus-dvertising
Day 7 Pointer arithmetic in C Programming language
Previous Home Next

# Pointer arithmetic in C Programming language

Arithmetic operations on a pointer is possible. There are four arithmetic operators that can be used on pointers: ++, --, +, and -

To understand pointer arithmetic, let us consider that ptr is an integer (Assuming 32-bit integers) pointer which points to the address 1000.

Let us perform the following arithmetic operation on the pointer:

int *ptr;
ptr++;

After above operation, ptr will be point to the next location 1004. The reason behind that, each time ptr incremented and it will located to next integer which is 4 bytes ( i.e. 1004) next to previous location (i.e. 1000). This operation doesn’t impact on actual value at memory location, Means value at memory location not changed.

In case of char:
char *ptr;
ptr++;

If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.

# Incrementing a Pointer

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  variable[] = {10, 100, 200};
   int  i, *ptr;

   /* let us have array address in pointer */
   ptr = variable;
   for ( i = 0; i < MAX; i++)
   {

      printf("Address of variable[%d] = %x\n", i, ptr );
      printf("Value of variable[%d] = %d\n", i, *ptr );

      ptr++;/* move to the next location */
   }
   return 0;
}
Output

Address of variable[0] = 2d5a5180
Value of variable[0] = 10
Address of variable[1] = 2d5a5184
Value of variable[1] = 100
Address of variable[2] = 2d5a5188
Value of variable[2] = 200

# Decrementing a Pointer

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  variable[] = {10, 100, 200};
   int  i, *ptr;

   /* let us have array address in pointer */
   ptr = variable;
   for ( i = 0; i < MAX; i++)
   {

      printf("Address of variable[%d] = %x\n", i, ptr );
      printf("Value of variable[%d] = %d\n", i, *ptr );

      ptr--;/* move to the next location */
   }
   return 0;
}
Output

Address of variable[0] = 6bc7f248
Value of variable[0] = 200
Address of variable[1] = 6bc7f244
Value of variable[1] = 100
Address of variable[2] = 6bc7f240
Value of variable[2] = 10

# Pointer Comparisons

Pointers may be compared by using relational operators, such as ==, <, and >.

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;

   ptr = var;
   i = 0;
   while ( ptr <= &var[MAX - 1] )
   {

      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );

      ptr++;
      i++;
   }
   return 0;
}

Address of var[0] = b0073c40
Value of var[0] = 10
Address of var[1] = b0073c44
Value of var[1] = 100
Address of var[2] = b0073c48
Value of var[2] = 200

Previous Home Next