Array problem

This is a discussion on Array problem within the C Programming forums, part of the General Programming Boards category; Which element(s) of the array e remain undefined at the end of the sequence? int e[6]; int *p1; int *p2 ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    3

    Array problem

    Which element(s) of the array e remain undefined at the end of the sequence?

    int e[6];
    int *p1;
    int *p2 = e;

    *p2 = 4;
    *(p2+1) = 8;
    p1 = p2 + 3;
    *p1++ = 2;
    *(p1+1) = 3;

    Please give detailed explanation. Thanks!

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Please give detailed explanation.
    You make it sound like you're copying a homework assignment ._.

    For the question, write it out on paper. I'll give you an example on how to start:

    Let's say the array "e" is at a random memory location, say 0x0 to make things easier. P1 is uninitialized and p2 is pointing to the same location, 0x0. Now, let's go through the steps:
    • Code:
      *p2 = 4;
      You're dereferencing p2 at where it points to, so the contents of memory location 0x0 is 4. You could also say that e[0] is 4.
    • Code:
      *(p2+1) = 8;
      P2 points to 0x0, so by adding one before dereferencing, you're accessing the contents of 0x1. You could also use array notation by saying p2[1], or e[1] as they both point to the same place.


    And continue this for the rest of the steps.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    Thanks for your reply. It is not an assignment. I dont understand even I have the rough answer, which is e[2] and e[4]... /_\
    Would you please explain the meaning of "p1 = p2 +3"? What is p1? Thank you.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    ._.

    Here, read this link. It explains everything you need to know about pointers and arrays, and a lot of the stuff looks pretty similar to what you have.

  5. #5
    Registered User
    Join Date
    Jun 2012
    Posts
    3
    I see. I have a clearer picture now. Thank you :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem initializing a double array for large array
    By gkkmath in forum C Programming
    Replies: 4
    Last Post: 08-25-2010, 09:26 PM
  2. Problem converting from char array to int array.
    By TheUmer in forum C Programming
    Replies: 11
    Last Post: 03-26-2010, 12:48 PM
  3. array problem
    By nynicue in forum C Programming
    Replies: 3
    Last Post: 02-25-2009, 10:34 PM
  4. simple array of char array problem
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2006, 01:04 PM
  5. problem with array
    By julianluthor in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2003, 05:18 PM

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