Thread: Fill the blanks in the program...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    54

    Fill the blanks in the program...

    Hai friends i am new to this forum . I have one problem please solve it ...
    Code:
    #include<stdio.h>
    void fun(.........);/*fill the blanks in this line*/
    main()
    {
    int *p=1000;
    fun(..........);/*fill the blanks in this line*/
    printf("%d\n",*(int*)p);
    }
    void fun(..........)/*fill the blanks in this line*/
    {
    .........
    ........./*fill the function body*/
    }
    friends this is the program you should not change any lines in this program and you want to fill the blanks which i had mentioned as (..........). And i need 2000 as the output from the printf in main.....
    Please solve it....

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you sure that is what you were given? It looks wrong to me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Yes this what the interviewer had given to me....

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And do you actually think that if you can't solve this task, you should have the job?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I tryed many things but i am not able to solve it.Please solve it...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your best attempt so far.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Hai friends i had tryed this but i got the segmentation fault
    Code:
    #include<stdio.h>
    void fun(int **q);
    main()
    {
    int *p=1000;
    fun(p);
    printf("%d\n",*(int*)p);
    }
    void fun(int **q)
    {
    **q=2000;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try removing one of the stars in fun().

    Edit: And there are more things wrong: You need to pass &p to fun(), and printf shouldn't have *(int *)p, but just p.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I had tryed this also but i am getting the same error . And you had told that to change in printf . But it is not possible they gave me the conditio that i should not change any line given by them
    Code:
    #include<stdio.h>
    void fun(int *q); 
    main()
    { 
    int *p=1000;
    fun(&p);
    printf("%d\n",*(int*)p);
    }
    void fun(int *q)
    {
    *q=2000; 
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ah, ok. It's a bit more complex then...

    You'll have to set *q = <some static variable>; and make the static variable 2000. And you DO need two ** on q into fun, and a &p when you call fun.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Can you please write the program....I am not able to get it....

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It would seem that your program stub is fundamentally flawed before you've even begun.

    > int *p=1000;
    This gets me
    warning: initialization makes pointer from integer without a cast
    You can't just invent memory addresses and expect to be able to use them.

    > printf("&#37;d\n",*(int*)p);
    Here you're dereferencing the pointer (which is currently pointing at some hard coded address).

    So you need to do two things
    1. Make p point to an actual integer, not just some invented address 1000
    2. Make the integer being pointed to contain the value 2000

    Post #7 was sort of on the right lines.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Don't take the job.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    I am new at programming and even I can tell you that this code is seriously wrong. Also if you can't figure it out, then don't take the job as this is probably the minimum requirement for the job... Just my opinion on this. Lemme try tearing this up.

    Code:
    #include<stdio.h>
    
    
    void fun(.........);/*fill the blanks in this line*/
    main()
    {
    
    int *p=1000;  <-- you declare an address but no variable to take it. You cant imagine an address out of nowhere. 
    
    This is what it is saying, "int (address?) = (undefined)pointer -> 1000;"
    
    fun(&p);
    printf("&#37;d\n",*(int*)p);
    }
    void fun(int *q) <--- why use q now when you could redefine p?
    {
    *q=2000; 
    }
    }
    I can't guarantee that what I noted was correct, but I think it is in the step in the right direction. Dont take the job mate if you can't do this minimum problem.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I thought that this forum members will help me to understand this full program . But no one is helping....So ok...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 10:31 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM