Pointers

This is a discussion on Pointers within the C++ Programming forums, part of the General Programming Boards category; I have the following questions: For parts b-e, repeat the declaration and use the results from a. Code: int x ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Pointers

    I have the following questions:
    For parts b-e, repeat the declaration and use the results from a.



    Code:
    int x = 6, y = 5, *px, *py;
    a) Write 2 assignment statements that set px and py to point at integers x and y. (I think it is *px = &x, *py = &y

    b) Write an output statement using px that print the contents of x. (I have cout << *px << endl

    c) Write a statement using py that assigns 20 as the contents of y. (I have *py *=4

    d) What is the resulting value of *py + *px? (Would it not be simply 11 using the results from part a)

    e) After executing *py = *px, are &x and *py equal? (I say yes. py now equals what px was pointing to which was &x)

    I am learning all of this online and just want to make sure that I am doing this right. Thanks

    BTW, I tried running this on my compiler but to no avail.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    a) that is incorrect.
    c) if you had properly assigned py to the address of y, then that gives you the number 20, but you are not following the instructions.
    e) that is incorrect also. same mistake as in a.

    the lines generating the errors should tell you which parts you have made mistakes in.

    socratic hint:

    what is the result of &x ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 11:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

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