search
Log In
0 votes
323 views

What will be the output of the below code? the answer given is E)0 but I am not getting it.
 

#include <stdio.h>
void fun(short int *a,char *b)
{
    b += 2;
    short int *p = (short int*)b;
    *p = *a;
}


int main()
{
    void (*fptr)(short int *,char *)
    short int a = 101;
    char arr[] = {'a','b','c','d'};
    fptr = fun;
    (*fptr)(&a,arr);
    printf("%d", arr[3]);

    return 0;
}


$A)$ Compilation error.

$B) 100$

$C)$ Garbage Value

$D)$ Segmentation Fault.

$E) 0$

in Programming
edited by
323 views

1 Answer

1 vote
 
Best answer
 

go through these links before reading answer if these concepts are not clear

SO:

https://stackoverflow.com/questions/12279060/what-is-the-difference-between-short-int-and-int-in-c 

 

 

A[0](1000+0) A[1](1000+1) A[2](1000+2) A[3](1000+3)
a b c d

short int *p = (short int*)b;

what this line does is assigns address 1002 in "p" in such a way that it will take 2 bytes 1002 and 1003(since it takes 2 bytes)

Now, when we assign 101 to it because of little-endian format, the higher order byte having 0 will be placed in right (A[3]) and lower order byte having value 101 at A[2], What you can do to test this is run the same program with 300 O/P would be 1 because higher order byte has 1(requires 256) 

Endian 

https://www.geeksforgeeks.org/little-and-big-endian-mystery/

 


edited by
0

Thanks @Anuj Mishra but, is it syntactically correct to assign fun to fptr?

0

Yes it is correct (function name itself is like an array name - so you can remove & and it still works - definitely go through this for reading and examples-http://www.cs.cmu.edu/~guna/15-123S11/Lectures/Lecture14.pdf

Also if  times, search wiki for function pointer - there you'll get a good example and some important usage too

0

@Anuj Mishra

Please Separate both the links in the post. They are showing as one url.

0

Thanks @logan1x , edited now.

0

@Anuj Mishra

Can u plz explain this line

fptr = fun;

Here fptr is a pointer, which should take address of 'fun'. Is it not?

0
Yes it is, read my above comment and go through link in that comment
0

How a function can be used as an array?

Even in array we use sign. isnot it?

0

No. We don't use & for referring whole array. Go through the first answer, same is case for functions ( please read above link if you haven't )

https://stackoverflow.com/questions/1641957/is-an-array-name-a-pointer

Related questions

1 vote
1 answer
1
178 views
Please suggest on below 2 points . I'm not able to understand those 2 points. Char s[100]; 1.What is difference between scanf("%s",s) and scanf("%[^\n]s",s)? 2.how below two codes are same Code 1 Getchar(); Scanf("%[^\n]s",s); Code 2 Scanf("%*[\n] %[^\n]",s); Thanks
asked Aug 30, 2018 in Programming Mayankprakash 178 views
0 votes
1 answer
2
182 views
#include <stdio.h> int main () { int c; while ((c = getchar ()) != EOF) { putchar (c); printf ("*"); } return 0; } when i run this program, the output i get is as qwerty q * w * e * r * t * y * * i'm not getting how this last "*" is ... the thing is, why the last * is getting printed in the next line. is it like putchar() returns and shift the printing pointer to new line? please clear my doubt..
asked Jul 21, 2018 in Programming aambazinga 182 views
0 votes
1 answer
3
317 views
The Correct answer is c but how? int main() { int a=210,b=120,c; c=a>b?1,2,3:2,5,6,7; printf("%d",c); } ans:- a) 1 b) 2 c) 3 d) Error.
asked Mar 31, 2018 in Programming dixit bishwash 317 views
1 vote
1 answer
4
173 views
#include <stdio.h> #include <conio.h> main( ) { int i = 3, j = 4, k, l ; k = addmult ( i, j ) ; l = addmult ( i, j ) ; printf ( "\n%d %d", k, l ) ; } addmult ( int ii, int jj ) { int kk, ll ; kk = ii + jj ; ll = ii * jj ; return ( kk, ll ) ; }
asked Nov 12, 2017 in Programming Parshu gate 173 views
...