Fill the blanks in the program...

This is a discussion on Fill the blanks in the program... within the C Programming forums, part of the General Programming Boards category; First of all, you don't think you're being absolutely silly to demand we finish a program to be used by ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    First of all, you don't think you're being absolutely silly to demand we finish a program to be used by you to demonstrate to someone that you are competent enough to be hired?

    Secondly, matsp and Salem both gave you ample information in order to solve this. Don't ignore what they have been saying.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    I am not able to understand the concept so please explaine me in detail. Then i will be able to get it...

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The concept is to make p point to valid memory that contains the value 2000.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by legendcrafter View Post
    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("%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.
    Just like access address 0, it is actually perfectly fine to set a pointer to 1000, -1, 4600, 9991 or any other value that fits in the size of a pointer [1] - the compiler may warn that you are setting a pointer to an integer without a cast - and that probably should be fixed if you want to make the code "nice".

    However, to make the code work, you'd obviously need to set the pointer to actually point to a bit of memory, and if you want it to output 2000, that bit of memory should be 2000.

    It is a bit of a "trick" piece of code, obviously designed to look simple at first, and fool someone to think that the answer is to just multiply by 2 - which wouldn't work in a general and protected environment.

    [1] This is a very common procedure when accessing hardware registers in drivers: You set a pointer to the address of the hardware register, e.g
    Code:
      volatile UINT32 *pCommand = (UINT32 *)(0xE000F000);
      volatile UINT32 *pStatus = (UINT32 *)(0xE000F004);
    ...
      *pCommand = myCommand;
       while(*pStatus & 0x04)  /* Not ready yet ... wait here */ ;
    ...
    --
    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. #20
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    void fun(.........);/*fill the blanks in this line*/
    The function needs to take a modifiable pointer to int (hint: the address of the pointer) and point it to an integer that contains the value 2000. This integer must survive even when the local function variables go out of scope.

    If you cannot solve this on your own, I strongly suggest you don't take this job.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Writing a C program for class....
    By Bizmark in forum C Programming
    Replies: 10
    Last Post: 11-13-2007, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21