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. Programming in C programming-in-c + – himgta 2.5k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Rishabh Gupta 2 commented Aug 9, 2017 reply Follow flag This question is from MADEEASY test series. 0 0 replyShare Shiva Sagar Rao commented Apr 13, 2021 i edited by soujanyareddy13 Apr 18, 2021 reply Follow flag This is a bad question because of reason explained by arjun sir in this comment You should not use an integer pointer to point to a character array or else behaviour is undefined in C. This is because C language supports memory alignment for integers where any integer can be assumed to start from an address which is a multiple of sizeof int. 0 0 replyShare Please log in or register to add a comment.
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. Tarun kushwaha 1 answered Mar 10, 2018 • selected Mar 11, 2018 by srestha Tarun kushwaha 1 comment Share Follow See all 7 Comments 7 7 Comments reply Show 4 previous comments akash.dinkar12 commented Mar 10, 2018 reply Follow flag before this, u should know the how addresses are going to be incremented because u might have seen such questions like some pointer incrementation or some questions like what kind of pointer it is whether it is character pointer or float pointer or integer pointer so depending on the type or size of data type u have to increase the step size in array type questions.. 0 0 replyShare srestha commented Mar 11, 2018 reply Follow flag @akash here the code is while(*++p1) So, the starting address gets incremented if the code is while(++*p1) then ascii character of 'G' will be incremented right? (just asking) 1 1 replyShare akash.dinkar12 commented Mar 11, 2018 reply Follow flag yes because * and ++ (pre-increment) are both of same priority but a right to left associative. 1 1 replyShare Please log in or register to add a comment.
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 just_bhavana answered Aug 9, 2017 just_bhavana comment Share Follow See all 2 Comments 2 2 Comments reply jatin saini commented Aug 19, 2017 reply Follow flag 1.How we can assign character pointer to integer,its giving error. 2.difference in pointer give no of elemets b/w them.Here they are 6. 0 0 replyShare bhuv commented Aug 19, 2017 reply Follow flag pointer passed is char pointer to int pointer receiving function, even if implicit conversion takes place and code run. you have taken char as 1 bytes(even though in question it is mentioned all data types are 2 bytes) and then suddenly divide by 2 as the size of an int. The division is done by SIZEOF(OBJECT). 0 0 replyShare Please log in or register to add a comment.