edited by
1,112 views
1 votes
1 votes

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.

edited by

2 Answers

Best answer
3 votes
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 votes
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

Related questions

1 votes
1 votes
0 answers
2
newdreamz a1-z0 asked Jan 18, 2019
337 views
i have 1 doubt regarding the initialization of highlighted portion.Will the value of ’k’ change at each iteration or it will remain same (whatever value assigned to i...
4 votes
4 votes
3 answers
3
newdreamz a1-z0 asked Jan 30, 2019
1,633 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 votes
0 votes
0 answers
4
balchandar reddy san asked Jan 20, 2019
491 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