• edited by
2,532 views
3 3 votes

Consider the following code

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

The output of given program is_________. Assume object of data type int occupies 2 bytes.

1 Answer

0 0 votes

First of all in line int fun(int *p) it should be int fun(int *p1)  Edit done

Now str is pointer to string constant which is typecasted to integer pointer in  fun(int *p1) therefore p1 and p2 are both integer pointers.

Line while(*++p1);  incements p1 pointer 2 bytes everytime till we reach the null value = 0 and this loop terminates.

p2-p1 = {addressof(p2) - address of(p1) } / 2     since integer occupies two bytes

p2 - p1 = -3   // p2 is at lower address and p1 is at higher address

-3 is returned and printed

Position:
Show:

Related questions

0 0 votes
1 1 answer
2.3k
2.3k views
kislaya Pant asked Jun 17, 2018
2,250 views
It is not any gate question but just my doubt over the topic C programming?Ques:- What will be the output of the following program?void main(){ int a=0,x=0; a =...
–2 –2 votes
0 0 answers
688
688 views
Sunil8860 asked Sep 21, 2017
688 views
1 1 vote
0 0 answers
714
714 views
saipriyab asked Nov 21, 2017
714 views
#include <stdio.h main() { int a,b,c; int count = 1; for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\ TFy!QJu ROo TNn(ROo)SLq SLq ULo+\ UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\ N...
0 0 votes
1 1 answer
2.4k
2.4k views
VS asked Dec 14, 2017
2,434 views