The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

C++ Help

JeiceJeice regular
edited September 2009 in Help / Advice Forum
int array[10];

for (int i =1; i <= 10; i ++)
      array[i] = i;
 

There's suppose to be something wrong with the above code. I figure it's because the array has 10 slots, but the first slot starts at 0, so the correct code should be:
int array[10];

for (int i =0; i < 10; i ++)
      array[i] = i+1;

Is that right?


2) We have the following variables in the following addresses

p is at address 100012
q is at address 100008
r is at address 100004
s is at address 100000


and then we have
int p = 100;
int q = 200;
int *r = 0;
int **s = 0;

r = &p;
s = &r;

/* what do the following print?

print (r)
print (*r)
print (s)
print (*s)
print (**s)

*/


What I got is

r prints 10012
*r prints 100
s prints 10004
*s prints 10012
**s prints 100

is this right? Can someone please help me visualize it with a diagram. The diagram I draw is this, but I don't know what r is pointing to, or what s is pointing to. I'm getting confused with the int **s = 0.
r [] at address 10004                  p [100] at address 10012

s [] at address 10000                 q [200] at adress 10008


I also have the following diagram, can someone help put it into code
pointer to int                                      int

p1 [100] at address 50  --------------> x [5] at address 100
p2 [200] at address 60 ---------------> y[7] at address 200
p3 [0] at address 70                           null pointer

My solution
int x = 5
int y = 7

int *p1 = &x;
int *p2 = &y;
int *p3 = 0

Jeice on

Posts

  • ChanusChanus Harbinger of the Spicy Rooster Apocalypse The Flames of a Thousand Collapsed StarsRegistered User, Moderator mod
    edited September 2009
    For 1, yes... if it's <= 10 then you will overrun the bounds of your array.

    << Edit: Is "array = i+1;" placing the value of i in your array so that it will read 1, 2, 3, 4, 5... 10? because that's what it will do.. =)


    For 2... I dunno. =)

    Chanus on
    Allegedly a voice of reason.
  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited September 2009
    You have it right (except your values for r, s, and *s are missing a 0). Here's how I would describe it.

    100012 p = 100
    100008 q = 200
    100004 r = 100012
    100000 s = 100004

    * notation is confusing, because it means two different things in two different contexts.

    In a declaration, it declares the type of a variable to be a pointer. Example:

    int p; //p is an integer
    int *p; //p is a pointer to an integer
    int **p; //p is a pointer to a pointer to an integer

    But anywhere else it means get the value of. Example:

    *p; //get the value at the location p points to
    **p; //get the value at the location that the value at the location that p points to points to

    'course, if you understand that last line, you probably don't need my help. A step-by-step example with s:

    *(*s); //equivalent to **s (I think). *s says, "get the value that s points to" - s points to r, or 100004. The value there is 100012. We now have *(100012), which says "get the value at 100012", which is 100.

    Note: I'm pretty sure *(100012) won't actually work, so don't try it.

    Hopefully that clears it up a bit.

    The last one looks right. It doesn't reflect the addresses provided by the diagram, but it probably shouldn't.

    admanb on
  • admanbadmanb unionize your workplace Seattle, WARegistered User regular
    edited September 2009
    By the way, is "p1 [100]", "x [5]", etc, your professor's notation? Because it looks like array notation, even though it's clearly not, which confuses things. :P

    admanb on
  • JeiceJeice regular
    edited September 2009
    Oh the [] is suppose to be a box, I just didn't know how to upload a picture or make a box -_-"

    But stepping away from the problem and re-looking at it now, I finally understand it. Thanks guys!

    Jeice on
Sign In or Register to comment.