Pointer assignment

This is a discussion on Pointer assignment within the C Programming forums, part of the General Programming Boards category; Hello to everyone, this is my first post of many to come :-) I was looking for a solution for ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Pointer assignment

    Hello to everyone,
    this is my first post of many to come :-)
    I was looking for a solution for the following program.

    Code:
      main()
    {
        int **ptr;
        int i;
        ......
        .....
        printf ("i = %d \n",i);
    
    }
    without assigning anything to the interger variable, we have to get the print statement to print
    a value (say 10),

    How to assign address of a variable to a pointer to a pointer???

    Thanks
    MAx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    I suggest that you use a pointer to an int instead. Also, note that main() returns an int, so you should declare it as such.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you really have to use the **int, assign the **int to an *int and the *int to the int.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Quote Originally Posted by C_ntua View Post
    If you really have to use the **int, assign the **int to an *int and the *int to the int.
    I wish i cud do that.

    those are the only two variable i can use (unfortunately :-( )

    this was a question asked in an interview..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    In that case you should use malloc() so that ptr will point to a new pointer to int, then assign the address of i to this new pointer to int, then finally assign 10 to the int. Oh, and free(ptr) when you are done, of course.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Or better cast!
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    int **ptr;
    int i=10;
    ptr = &i;
    printf("&#37;d", *(int *)ptr);
    return 0;
    }
    you ll get a warning but no harm done. The result is correct

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Or better cast!
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    int **ptr;
    int i=10;
    ptr = &i;
    printf("%d", *(int *)ptr);
    return 0;
    }
    you ll get a warning but no harm done. The result is correct
    That breaks:
    without assigning anything to the interger variable.
    I also would call that "cheating" if I was the interviewer. Laserlight's solution is correct, or the interview question is incorrect.

    --
    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.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Or better cast!
    If I were the interviewer I would think twice before hiring someone who so gleefully chooses a hack over a solution, and clearly has no notion of what it means to keep code formatted so that it would be readable.

    EDIT:
    Oh yes, and while I was annoyed by the glee at a hack, I missed the point that the hack is invalid to begin with. Oh well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Oh, my bad. I didn't get it exactly. But anyhow you could just do *((int *)ptr) = 10

    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.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Oh, my bad. I didn't get it exactly. But anyhow you could just do *((int *)ptr) = 10

    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.
    It's cheating, because you are essentially just CHANGING the meaning of the variable that was originally declared, rather than using it with the meaning that it has been declared as. There are valid cases where you would cast a pointer to another type, but to solve the above code, it's not necessary, and thus your solution is "not good". It's a bit like when trying to fit a round peg in the square hole, instead of finding the round hole, you take out a drill and make the hole round - not the right solution. That's my personal opinion.

    --
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    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 :-)

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. main returns int - should be

    Code:
    int main(void)
    see FAQ

    2. No need to cast malloc in C - see FAQ

    3. You should check the return value of malloc before use
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Quote Originally Posted by vart View Post
    1. main returns int - should be

    Code:
    int main(void)
    see FAQ

    2. No need to cast malloc in C - see FAQ

    3. You should check the return value of malloc before use
    well, didnt care for that as this was just an interview question

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    didnt care for that as this was just an interview question
    It is an additional reason to care about such things...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 2 12 LastLast
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, 11:10 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  3. Replies: 3
    Last Post: 06-19-2004, 11:24 AM
  4. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 11:08 PM
  5. When do we get Null Pointer Assignment
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 02:48 AM

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