• edited by
2,541 views
1 1 vote

Consider the following program segment

int main()
{
    char *str = "GATECS";
    printf("%d",madeeasy(str));
    return 0;
}
int madeeasy(int *p1)
{
    int *p2=p1;
    while(*++p1);
    return(p1-p2);
}

The output of the above program will be _____ . Assume that the object of data type $int$ occupies $2$ bytes.

2 Answers

Best answer
3 3 votes

Ans 3

p2 will point to 0 th position i.e G in string array.

after while loop p1 will point to end of string i.e null \0 which is at position 6.

So, 6-0/2 = 3.

why divide by 2 because it is integer pointer and int is given of size 2 byte and char is of 1 byte so one int chunk can accommodate 2 bytes.

• selected by
2 2 votes

3 will be printed.

b will point to 'G' and a will point to null string ( \0 )

For subtraction of two pointers, if p1 and p2 are both pointers of type T *, then the value computed is:

    ( p2 - p1 ) = ( addr( p2 ) - addr( p1 ) ) / sizeof( T )

In the context of your program, let base address of the string be 2000. So, b = 2000 and a = 2006 (as char will take 1 byte)

Now, a - b = $\frac{address(a) - address(b)}{size of integer}$  = $\frac{2006 - 2000}{2}$ = 3

Position:
Show:

Related questions

0 0 votes
1 1 answer
774
774 views
saket jaiswal asked Jan 7, 2025
774 views
#include <stdio.h void swap(int x, int i, int j) { int *temp; temp = x[i]; x[i] = x[j]; x[j] = temp; } int main() { int a[6] = {1, 2, 3}; int (*p)[6] = &a; int *arr[3] ...
4 4 votes
3 answers 3 answers
3.3k
3.3k views
newdreamz a1-z0 asked Jan 30, 2019
3,277 views
Consider the following function:void madeeasy (int n) { if (n < 0) return; else { printf(n); madeeasy (- -n); madeeasy (n - -); printf(n); } }The sum of all values printe...
0 0 votes
0 0 answers
1.1k
1.1k views
balchandar reddy san asked Jan 20, 2019
1,137 views
Let X be the number of times the comparison inside the while loop (i.e. m <= n ) is performed, when mystery(127, 255) is called. Then the value of X is,125656466