Thread: Pointer assignment

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why is this cheating? The interviewer doesn't specify WHY you cannot declare another variable. If it is for lack of memory then you couldn't use malloc(). If it is a problem of not wanting to have another variable then malloc is far better. Depends.
    That's why it is an interview question, not a real question to be solved (in which case I would discard the pointers and maybe even discard the int variable). By ignoring the underlying problem: that you have been presented with an int** instead of the more natural int*, it shows that your hack misses the point of the question. I call it a hack because it involves two unnecessary reinterpret casts, one of which you did not make explicit and thus the compiler reported the warning.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Posting the code,
    might be of some help for somebody.

    Code:
    main()
    {
        int **n,p;
    
        n = (int **) malloc (sizeof(int *));
        (*n) = &p;
         **n = 30;
    
        printf(" %d \n",p);
    
    }
    @laserlight Thanks :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Replies: 3
    Last Post: 06-19-2004, 10:24 AM
  4. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 10:08 PM
  5. When do we get Null Pointer Assignment
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 01:48 AM