Thread: A little help with lists please...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    A little help with lists please...

    Hi there

    I am busy swatting, but have hit a speed-bump. It has to do with reversing arrays whilst not allowing use of the array brackets (doing it with pointers in other words).

    Basically, I have written this.

    Code:
    void reverse (int *numbers, int lwb, int upb) { 
    
    int *p1, *p2, temp;
    
    p1 = numbers + lwb
    p2 = numbers + upb
    
    while (p1 < p2) {
    temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    *p1++;
    *p2--;
    }
    ...in a bid to swap them. Swapping opposite ends until the pointers are at the same memory address.

    However, I've been told that the following would work too. Can you verify?

    Code:
    while (p1 < p2) {
    temp = *p1;
    *p1++ = *p2;
    *p2-- = temp;
    It's just that to me, you're trying to do two things at once with the pointers. Does this not mean that the pointer increments and then assigns the value?

    Cheers

    M

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Does this not mean that the pointer increments and then assigns the value?
    No, that would be *(++p1) = *p2;
    Yes, the () are necessary on the pre-increment version of the expression.

    As written, you do the assignment, THEN increment the pointer.


    Code:
    while (p1 < p2) {
      temp = *p1; 
      *p1 = *p2;
      *p2 = temp;
      p1++;
      p2--;
    }
    Is probably just as good as anything more condensed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    Sorry...

    Salem, thanks for the reply, but I am not clear. Are you saying that the second piece of code in my first post is actually correct? And are both versions of the while loop going to work anyway?

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10
    Ok, we lost Salem, anyone? Can you tell me if the while loop in both cases is correct?

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the while loop that Salem gave you seems most correct. The reason I say this is because:
    Code:
    *p1 = /* the value stored in the memory location of p1  */
    p1++ = /* assigns p1 to the very next memory location */
    *(++p1) = /* increment the pointer, assign the value */
    *p1++ = /* assign the value of p1 and then increment the pointer */
    There is quite a difference in the syntax of your statements, so Salem is most correct.
    Last edited by whiteflags; 05-08-2006 at 12:38 PM.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Location
    Cork
    Posts
    10

    hmm

    OK, thanks Citizen. I just didn't understand his reply. So what you are basically saying is that both are correct, but my second while loop is slicker. Excellent.

    You learn something new everyday. I was worried that dual increment/assign in one command could get messy, but thanks to the two of you I now have the rules governing both states.

    many thanks.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > So what you are basically saying is that both are correct
    No, the pre-increment / decrement versions will fail to swap the first/last elements.

    I suggest you try both forms as a study exercise
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Salem, even though you're right, you took his comment out of context. Both the while loops should work.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I will never understand people that post questions like this:
    I've been told that the following would work too. Can you verify?
    Why should I verfiy it? Write some test code and try it yourself.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  2. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM