Awesome q2a theme

User Sambhrant Maurya

Verification: Verified Account
Member for: 1 year (since Mar 28, 2018)
Type: Registered user
Full name: Sambhrant Maurya
GATE Year: 2020
GATE Rank:
Website: ealliance.org
About: B.Tech (ME)
Geek, environmentalist
Resume:
DBLP Link:
Following standard books?: Yes
Current City: Lucknow
Home State:
Donate Me Link:

Activity by Sambhrant Maurya

Score: 290 points (ranked #17)
Posts: 0
Questions: 90 (10 with best answer chosen)
Answers: 8 (3 chosen as best)
Comments: 115
Voted on: 10 questions, 27 answers
Gave out: 28 up votes, 9 down votes
Received: 34 up votes, 1 down vote

Wall for Sambhrant Maurya

https://gateoverflow.in/129778/test-by-bikram-2017-programming-test-2-18#c316933

A void function cannot return a value though it can have a return statement.
So, the answer is B .



yes you are correct, when we use void as return type it makes Return keyword as of no use ..

hence option B is correct, it provides error in return statement .

Reference:

http://stackoverflow.com/questions/17801793/function-cannot-return-a-value

even if u want to write a return statmnt for a void function(which is incorrect but lets suppose it compiles successfully )
 then also return should be written like return; and not return(m+2);

 
and also isnt it wrong that u have declred a function as void and then writing a return stmt to it.

#include <stdio.h>

void main(int n)
{
    printf("hello");
    return n;
}

gives output as  " hello "

so it return a value while return type is void .

even if u want to write a return statemnt for a void function(which is incorrect but lets suppose it compiles successfully ) then also return should be written like return; and not return(m+2);is it valid.
Jul 16 by Bikram
actually sizeof(int) is compiler dependent ..  so from this line

t = (p += sizeof(int))[-1];  
what we get is
if sizeof(int) = 2
[ in 16 bit systems it is 2 bytes, which is now obsolete we don't use 16 bit computer now a days]

then  
t = (p += sizeof(int))[-1]
t = (p = p + 2)[-1]
t = *(p + 2 -1) = *(p+1) = address of second element i.e. 'cd'

--------------
if sizeof(int) = 4 [ in 32 bit systems which is used now a days ]

then  

t = (p += sizeof(int))[-1]
t = (p = p + 4)[-1]
t = *(p + 4 -1) = *(p+3) = address of forth element i.e. 'gh' .
..................

#include <stdio.h>
void f(char**);  
int main()
{
char *argv[ ] = { "ab", "cd", "ef", "gh", "ij", "kl" };        // it creates character array.

     //   In char array address point to only starting of character array.


f(argv);               // function call with base address of char array.  
return 0;
}


void f(char **p)             // function call comes here. here p is pointer to pointer array

{
char *t;                             //create pointer variable
t = (p += sizeof(int))[-1];            //t = (p = p+4 ))[-1];

                                          // p point to "gh" starting of this char array.

                                       //       now P[-1] ; t= *(p-1); now t point to  "gh" array .

 
printf("%s\n", t);               // %s this tell to print string until % not encounter .

since every string stored end with %.
}

so it prints 'gh' .



    The size of an int is really compiler dependent. Back in the day, when processors were 16 bit, an int was 2 bytes. Nowadays, it's most often 4 bytes on a 32 bits system or 8 bytes on 64 bits system

    http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes

..................
 size of int we should take in 32 bit systems as 4 Bytes .

yes, 2 Bytes was used previously in 16 Bit systems but now we mostly use 32 Bit systems so size of int is 4 Bytes .
Jul 15 by Bikram
z=f(&i);

this correct ..

even if u want to write a return statmnt for a void function(which is incorrect but lets suppose it compiles successfully ) then also return should be written like return; and not return(m+2);

and also isnt it wrong that u have declred a function as void and then writing a return stmt to it.
Jul 15 by Bikram
you are correct, when we use void as return type it makes Return keyword as of no use ..

hence option B is correct, it provides error in return statement .

Reference:
https://stackoverflow.com/questions/17801793/function-cannot-return-a-value

A void function cannot return a value though it can have a return statement.
So, the answer is B.
Jul 15 by Bikram
2,314 questions
1,294 answers
6,591 comments
89,719 users