3,009 views
1 votes
1 votes

#include<stdio.h>

int main()

{

    int arr[] = {10, 20, 30, 40, 50, 60};

    int *ptr1 = arr;

    int *ptr2 = arr + 5;

    printf("Number of bytes between two pointers are: %d"

                              (char*)ptr2 - (char*) ptr1);

    return 0;

}

Please explain the above code

1 Answer

6 votes
6 votes
Elements 10 20 30 40 50 60
Address 1000 1004 1008 1012 1016 1020

Assuming size of int = 4byte and size of char = 1byte

ptr1 =1000

ptr2= 1000+5 {skipping 5 elements of size 4byte each}

=> ptr2 =1020

type casting is done here into char

(char*)ptr2 - (char*) ptr1);

1020-1000=20/size of char

o/p: 20

Related questions

3 votes
3 votes
1 answer
1
Storm_907 asked Apr 16, 2023
461 views
Please explain this question void main() { int a =300; char *ptr = (char*) &a ; ptr++; *ptr=2; printf("%d" , a); }
4 votes
4 votes
4 answers
3
Manoj Kumar Pandey asked May 22, 2019
819 views
https://gateoverflow.in/?qa=blob&qa_blobid=14433986388826671915int main() { int a = 10; int *b = &a; scanf("%d",b); printf("%d",a+50); }What will be the Output of the fol...