Thread: addres and pointer

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    addres and pointer

    This question was on exam today and i couldn't say the right answer. So i was given this --- int &y=x; and i should have added some code so the statement above would make sense. I thought like this: y contains an address so x should contain an address too. So it could be a pointer so the code to be added could be int *x; is this right? If not how should i think?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are saying that given a program like this:
    Code:
    int main(void)
    {
        /* #1 */
        int &y=x;
        return 0;
    }
    You are supposed to replace the comment #1 with code that will make the above program compile?

    Note that you posted in C programming forum. Is this exam question really concerning C, or is it supposed to be C++? In C, it looks impossible: maybe some macro trick to redefine int might work, but you'll need to figure out how to circumvent the fact that bitwise operator& does not result in a modifiable lvalue, so you cannot assign x to the result.

    In C++, that looks like the initialisation of a reference named y of type int& to refer to x, so the solution is trivial:
    Code:
    int x;
    int &y = x;
    EDIT:
    Or are you saying that given this line of code:
    Code:
    int &y=x;
    You are supposed to add characters to it such that it makes sense and could feasibly compile? If so, that seems terribly open-ended. For example:
    Code:
    integer && y == x;
    The above compares the result of logical && between two variables integer and y, with the variable x, to see if they are equal, but does nothing with the result.
    Last edited by laserlight; 01-15-2016 at 08:29 AM.
    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

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    My exam question was about pointers and addresses so I don't think that this has something to do with the bitwise & or comparison. And it was C, not C++. What about something related to pointers? Could it be int *x? Or something else containing a pointer?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lmanukyan
    My exam question was about pointers and addresses so I don't think that this has something to do with the bitwise & or comparison. And it was C, not C++. What about something related to pointers? Could it be int *x? Or something else containing a pointer?
    What exactly was the exam question? It makes no sense to me. I mean, it could become:
    Code:
    int *y = &x;
    or maybe:
    Code:
    int *x = &y;
    but who knows? As stated, the question is so vague and open to a variety of plausible answers. It is like asking you to make sense of this English sentence: "brown dog fox jumps lazy over quick the the".
    Last edited by laserlight; 01-15-2016 at 09:37 AM.
    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

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    The question was to add some piece of code to this

    Code:
    int &y = x;
    so that it would make sense. The teacher said this doesn't have a trivial answer. Maybe it's simple but not easy to just guess it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well, I wouldn't bother with this: this is a parlour trick trivia question that has nothing to do with C programming other than the C programming language forming the basis for it to be posed.

    I mean, if we want to satisfy the "pointers and addresses" thing, this would be valid:
    Code:
    int *p = &y; int q = x;
    or this one:
    Code:
    int z = *&y = x;
    If (either of) these are not valid, I assure you that I can pose your teacher a similiar question that he/she will not be able to answer correctly, time and time again. It is simply ridiculous and of no value to the learning of programming or the C programming language.
    Last edited by laserlight; 01-15-2016 at 09:59 AM.
    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

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by laserlight View Post
    It is like asking you to make sense of this English sentence: "brown dog fox jumps lazy over quick the the".
    Sounds like something from the recent AI thread.

  8. #8
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    That's what I first answered then he said that if this was a trivial question like that then he wouldn't get me 100 points for answering it. Well we've just started learning addresses and pointers and I hadn't practiced it very well, and I'll try to understand those more deeply, maybe then I'll understand. And one more question was - when an array is passed to the function, does the array "go" to the stack? I answered yes but yeah the right answer was no. Why though? When an array is passed to the function, it means the address of the first element of the array is passes? (Am I right?) and if so, does that first element "go" to the stack? If none of what I said is right, then where does the array "go" when it's passed to the function?
    Last edited by lmanukyan; 01-15-2016 at 10:06 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lmanukyan
    And one more question was - when an array is passed to the function, does the array "go" to the stack? I answered yes but yeah the right answer was no. Why though? When an array is passed to the function, it means the address of the first element of the array is passes? (Am I right?) and if so, does that first element "go" to the stack? If none of what I said is right, then where does the array "go" when it's passed to the function?
    This is at least a better question: the array does not "go" anywhere. You just passed a pointer to its first element. The storage allocated for it remains in the space allocated on the stack for the function call in which it was declared (or not in the stack at all, if it has static storage duration).
    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

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    So can the array (or the address of its first element) be in that space of stack which is allocated for the function to which the array is passed?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by lmanukyan
    So can the array (or the address of its first element) be in that space of stack which is allocated for the function to which the array is passed?
    The parameter corresponding to the pointer argument would be allocated storage in the stack frame of the called function.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-18-2015, 03:13 PM
  2. Replies: 8
    Last Post: 03-01-2015, 12:39 AM
  3. Broadcast struct in C. Change ip SOURCE addres.
    By franmf in forum C Programming
    Replies: 5
    Last Post: 12-11-2012, 05:07 PM
  4. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  5. Replies: 4
    Last Post: 08-27-2007, 11:51 PM