Thread: question on C program

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    3

    question on C program

    Hi,

    Code:
    void my_func();
    main() {
    int i = 5;
    my_funct(); /*- The definition of my_funct has to change the value of I*/
    printf("%d",i);
    }
    
    void my_funct(void) {
    ...
    }
    Please let me know how to do that?
    That is, my_funct body has to be written in such a way that it makes changes in the var i.

  2. #2
    Registered User
    Join Date
    Nov 2009
    Location
    Italy
    Posts
    65
    You ought to pass the i variable as a reference parameter, so my_funct() should be my_funct(int* i) and then you can access that variable by dereferencing it with the * operator

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Thanks for your reply,

    I knew it already,

    But here my_func is intentionally written as void function which has no arguments.

    It is an interview question.

    Experts, pl give a solution.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why should we give you a solution to an interview question??? The idea of an interview question is to get an idea of what you know prior to hiring you...if you don't know how to pass by reference in C, and the job requires it, you should not be hired.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    3
    Quote Originally Posted by rags_to_riches View Post
    Why should we give you a solution to an interview question??? The idea of an interview question is to get an idea of what you know prior to hiring you...if you don't know how to pass by reference in C, and the job requires it, you should not be hired.
    I told it clearly, this problem not at all related to pass by reference,

    my_func has no arguments. It is something related to stack smashing, from my_func stack, we need to find main function stack starting address, from there we need to find address of i, then modifying that value,

    I found this qn will be very useful others also,

    Anyway thanks for your complement, pl dont consider my CV in your high tech corporate.

    What is wrong in asking clarification, this is a forum. Please read the post then reply.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    That seems a bit complex for a interview question? How are you going to do that on paper without knowing the address to start at?

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >> related to stack smashing

    Sorry, but this sails a little to close against the rules we have about the discussion of hacking (C Board - Announcements in Forum : C Programming)

    If you disagree please feel free to PM me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM