in Technology by
How is Pointers subtraction in C?

1 Answer

0 votes
by

We can also subtract one pointer from another pointer. This technique is useful when we need to calculate the number of bytes between the two pointers. But pointers subtraction is useful when they are pointing the same array or memory block.

If subtract two pointers that pointing the same array, the result of this subtraction gives the number of elements that separate them. Let’s consider two examples which describe the subtraction of two number.

Example 1:

In this example, we will calculate the size of the structure to subtract the pointers.

 

#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int Age;
float Weight;
} sInfo;
int main()
{
//create an array of structure;
sInfo JhonFamilyInfo[2];
//Create pointer to the structure
sInfo *psInfo = NULL;
int iSizeofStructure = 0;
//Assign the address of array to the pointer
psInfo = JhonFamilyInfo;
// Subtract the pointer
iSizeofStructure = (char*)(psInfo + 1) - (char*)(psInfo);
printf("Size of structure = %d\n\n",iSizeofStructure);
return 0;
}

Output: Size of structure =  8

Related questions

0 votes
    In 10011 – 01010 (binary subtraction) what will be the answer and is there any need for borrowing pls share steps Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    When should we use pointers in a C program?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the advantage of pointers in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is void or Generic pointers in C?...
asked Jan 21, 2021 in Technology by JackTerrance
+1 vote
    What are valid operations on pointers in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What is the advantage of declaring void pointers in C-Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    Describe Wild Pointers in C Progamming?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    What are the advantages of using an array of pointers to string instead of an array of strings?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is an array of pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the difference between pointer to an array and array of pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Can we perform arithmetic operation on pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Where can the function pointers be used?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What ais the use of void pointers?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the difference between near, far and huge pointers?...
asked Jan 21, 2021 in Technology by JackTerrance
...