Thread: A couple questions on Pointer Assignment

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    A couple questions on Pointer Assignment

    I think I'm getting the hang of pointer basics with my textbook. I've read the chapter a couple times now and it's starting to make sense

    A few lingering questions from the exercises I completed

    First set (regarding picture directly below):

    Question i. : Are a). and g). indeed the only aliases for i? (I looked these up in an answer key that I'm not 100% sure is correct)

    Question 1. : Outside of function calls, when would you apply the & (address extraction) operator for an object that is not pointing to anything such as i? An example I can think of from the book (it was a one sentence allusion) printing an address number with the %p conversion spec in a format string.

    Same question in regard to the * (de-referencing) operator Example I can think of are printing the value of an object like this with printf. (Again, this was covered in about one sentence in the book)

    Question 2. : What is the different between g). *&i and h). &*i. Shouldn't the two operators cancel out since they are like the inverse of one another? And why is g). considered an alias, while h). is not?

    A couple questions on Pointer Assignment-222-jpg


    ----------------------------------------------------


    Second set (just one quick question)

    Question 3: Is my explanation of why b). is illegal correct? I found this one tricky at first but then looked at the text and examples under the orange text.

    A couple questions on Pointer Assignment-2-jpg
    Attached Images Attached Images A couple questions on Pointer Assignment-1-jpg 
    Last edited by potomac; 12-30-2016 at 10:30 PM.

  2. #2
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    OK let's work through these one at a time....

    Question 1. You use & when you need the address of something, i.e. for setting where a pointer will point to or for passing the address of an output parameter ( remember the pass by reference example) when a pointer is expected (you've done this in scanf's since the beginning almost of your study).

    Question 2 is subtle. It's down to operator precedence and associativity. Both * and & have the same precedence and the associativity for them both is right to left. so *&i is take the address of i and then dereference that address, basically they cancel out, whereas &*i says dereference i (treat i as if it was a pointer) and take the address of the dereferenced object.

    Address of is always safe, it depends what you go on to do with that address, but just taking an address is safe. On the other hand dereferencing something that is not valid is unsafe. You should only dereference a valid pointer, one of the right type for the object being pointed at. You can't dereference a void* because the compiler doesn't have a clue what sizeof(void) is.

    Question 3. Yes quite right. It's a type mismatch. *p is type int, &i is type int* assuming i is an int anyway. Type safety is your friend, it stops you doing silly things.

    Did I miss anything? Might have done, it's 5.45am here
    Last edited by Hobbit; 12-31-2016 at 12:11 AM.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Thanks hobbit. that's very helpful

    happy new year to you too

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    I think a lot of it comes down to right associativity. Up until this point in the book, we had only used left associative operators, not right ones like * and &. I flipped to the Appendix on precedence/associativity and it helps a lot
    Last edited by potomac; 01-02-2017 at 09:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just a couple of questions
    By redsfan in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2011, 10:13 AM
  2. A Couple of Questions
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 12-03-2005, 06:47 PM
  3. A couple questions
    By fenixataris182 in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2005, 05:13 PM
  4. A couple of OOP questions
    By codec in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2004, 07:18 PM
  5. New to C++/ Couple of questions...
    By danielthomas3 in forum C++ Programming
    Replies: 13
    Last Post: 04-14-2002, 03:46 PM

Tags for this Thread