Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6104499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:50:56+00:00 2026-05-23T13:50:56+00:00

This was the question asked to me in an interview in c: #include<stdio.h> void

  • 0

This was the question asked to me in an interview in c:

#include<stdio.h>
void main(){
  char *ch;
  ch=fun1();
  printf(ch);
}

fun1(){
  char *arr[100];
  strcpy(arr,"name");
  return arr;
}

I was given the above program and was asked to figure out the problems in the above code.
below was my answer

  • function declaration is wrong.the
    return type should be char **
  • syntax of printf is wrong
  • arr scope is limited to the function fun1

then

Interviewer : what would be your solution to the problem?

Me: you need to make the arr variable as global and fix the remaining issues mentioned above.

Interviewer: Dont you think global variables are dangerous?

Me: Yes ofcourse,since we cannot say where it is being accessed in which functions and sometimes it gets almost impossible to find which function has changed the value

Ineterviewer :give me a solution without a global variable

Me:????

what would be your solution for this?
Could anybody pls point out the errors that i have made !!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:50:57+00:00Added an answer on May 23, 2026 at 1:50 pm

    My solution could be

    #include <stdio.h>
    #include <string.h>
    
    /* copies "name" to `dst`.
    ** The caller is responsible for making sure
    ** `dst` has enough space */
    char *fun1(char *dst) {
      strcpy(dst, "name");
      return dst;
    }
    
    int main(void) {
      char ch[5];
      printf("%s\n", fun1(ch));
      return 0;
    }
    

    Here’s just what I found about the snippet …

    #include<stdio.h>
    

    I’d add a space before the header: #include <stdio.h>; but that’s just for looks

    void main()
    

    WRONG! main returns an int. ALWAYS!
    And it should be one of: int main(void) or int main(int argc, char **argv) if you need parameters

    {
    char *ch;
    ch=fun1();
    

    No prototype for fun1 in scope. This makes the compiler assume the function returns a value of type int and the statement tries to assign that value to an object of type char* which is illegal: the compiler must issue a diagnostic here.

    printf(ch);
    }
    
    fun1()
    

    In C99, it is mandatory to specify a return type; in C89, int is assumed. Also this function definition really should also be a prototype (in both C89 and C99) and specifiy the number and types of parameters (or void if it takes none).

    {
    char *arr[100];
    

    Why do you need an array of 100 pointers? ???

    strcpy(arr,"name");
    

    Oops … no prototype for strcpy() in scope. Also, assuming the strcpy is the one declared in <string.h>, the 1st parameter should be a char* not a char **

    return arr;
    

    arr ceases to exist right after the function returns. Its address (the array decays to the address of its first element in this context) is unusable in the calling code.

    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more
This question was asked in the written round of a job interview: #include<alloc.h> #define
This question was asked at interview. Say I have a contract. [ServiceContract] public interface
This was the question asked in interview. Can we call one constructor from another
This is an interview question asked a month ago.... Do session use cookies? If
I was asked this question in a job interview, and I'd like to know
Yesterday in my interview I was asked this question. (At that time I was
Hi i was asked in an interview about this question. I did google a
this is an interview question my friend was asked yesterday. The question was something
This question was asked in my interview. random(0,1) is a function that generates integers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.