0 0 votes closed with the note: bad question 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 out of above program will be ________. Assume that the object of data type int occupies 2 bytes. Programming in C programming programming-in-c made-easy-test-series pointers output bad-question + – Aibi 2.7k views comment Share Follow Print See all 4 Comments 4 4 Comments reply Prabhanjan_1 commented Oct 30, 2017 reply Follow flag Initially p1 points to GATECS int *p2 = p1 means p2 is pointing also pointing to GATECS while(*++p1); meaning at loop breaks when p1 is pointing to S. Now p1 is pointed to '\0'. Each integer takes 2 bytes.So calculating number of jumps , ans of p1-p2 is 3 as p1 is at higher address. 0 0 replyShare Jaspreet Kaur Bains commented Jan 2, 2018 reply Follow flag meaning of while (∗++p1)???? increment value of p1....till we reach last charcter (S out of GATECS) OR increment value of p1....till we reach NULL CHARACER(/0) 0 0 replyShare vamp_vaibhav commented Jan 5, 2018 reply Follow flag Jaspreet as you can see in the question That the address of GATECS is passed in str.. Nd nowhere GATECS is stored in array.. Therefore the pointer is incremented till it gets the value which will break the condition of while.. Now while is false only when we get null.. Therefore it will be incremented till we reach null character 0 0 replyShare Shiva Sagar Rao commented Apr 13, 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. Duplicate: https://gateoverflow.in/164145/programming 0 0 replyShare Please log in or register to add a comment.
1 1 vote ans will be 6 because we are doing pointer subtraction arun yadav answered Oct 15, 2020 arun yadav comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes Answer is 6. This function basically counts number of characters in a string. The function madeeasy(int *p1) basically counts number of characters in input string.. Inside madeeasy(int *p1) pointer p2 is initialized as p1. The statement while(*++p1) increments p1 till ‘ \ 0’ is reached! p1 is incremented by 6.. Finally the difference between p1 and p2 is returned which is 6. Ashwin Kulkarni answered Oct 30, 2017 Ashwin Kulkarni comment Share Follow See all 4 Comments 4 4 Comments reply Aibi commented Oct 30, 2017 reply Follow flag I am getting the same answer, i.e, 6. But Made Easy has given the answer as 3. :-( 0 0 replyShare Ashwin Kulkarni commented Oct 30, 2017 reply Follow flag Ohhk. i got it! Because we are pointing integer pointer to a string. So value returned will be p1-p2/sizeof(int). and that's why they have mentioned size of int specifically. OR when we're incrementing p1, it will increment by [p1+(1*sizeof(int))] and hence in 3 increments only we can reach to '\0'. because each time integer pointer will increment by 2 characters. 1 1 replyShare Aibi commented Oct 30, 2017 reply Follow flag thank you. this comment is more helpful than the answer. :-) 0 0 replyShare Arjun commented Oct 30, 2017 reply Follow flag 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. 2 2 replyShare Please log in or register to add a comment.
0 0 votes answer will be 3........as difference will be 6.....we will divide it by 2 as size of int pointer will be 2 bytes....thus 3.... himanshu19 answered Jan 4, 2018 himanshu19 comment Share Follow See all 2 Comments 2 2 Comments reply Jaspreet Kaur Bains commented Jan 5, 2018 reply Follow flag @himunshu sir what is meaning of while (∗++p1)???? 0 0 replyShare himanshu19 commented Jan 16, 2018 reply Follow flag increament the pointer till you get null.....so pointer will be increamented 6 times.....difference between these 2 pointers will be 6 bytes ...since it is int pointer and size of int pointer is 2 bytes....hence ans will be divided by 2......thus we will get 3...had it been char pointer ans would have been 6...since the size of char pointer is 1..... sorry for the late reply 0 0 replyShare Please log in or register to add a comment.