Thread: arrays and pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    arrays and pointers

    what will be the contents of the array SORT after the following statements are done??

    Code:
    #define ELEMENTS 7
    
    int SORT[ELEMENTS] = {2,4,6,8,10,12,14};
    int *s = &SORT[0], *t = &SORT[ELEMENTS -1], temp;
    
    while (s < t) 
    
    {    temp = *s;
          *s++ = *t;
          *t-- = temp;
    }
    I think the contents are unchanged.
    Is that correct??

    Or is this a trick question, and the contents are reversed??

  2. #2
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    They are reversed. The last element becomes the first and the first becomes the last, etc.

    before swap: 2, 4, 6, 8, 10, 12, 14

    after swap: 14, 12, 10, 8, 6, 4, 2


    No more homework questions.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    ARE you kidding or being facetious???

    I am just trying to understand the question??
    pointers are a bugger to get and C depends on knowing
    arrays and pointers.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well lets walk you through the code

    #define ELEMENTS 7 // replaces ELEMENTS with 7 before its compiled

    int SORT[ELEMENTS] = {2,4,6,8,10,12,14};// an array
    int *s = &SORT[0], // take a pointer to the start of the array
    *t = &SORT[ELEMENTS -1], // ditto the end of the array
    temp; // a tempory holding variable

    while (s < t) // will stop at the midpoint of array

    { temp = *s;// store the value pointed to by s in temp

    *s++ = *t;
    //is equivalent to....
    // *s=*t; // make the value pointed to by s equal the value pointed to by t
    // s++;// advance the pointer to next element in array
    *t-- = temp;
    // is equivalent to...
    //*t=temp;// make the value pointed to by t equal to temp
    //t--;//move pointer to next element backwards in array
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    23

    What I really wanted to know

    Stoned Coder ---------

    Thanks for the step by step, however,

    The original question was:

    what will be the contents of the array SORT after the following statements are done??

    And my thought was either the answer is the obvious, the elements are reversed as that is what the WHILE loop will do
    OR this is a trick sort of question, because the actual elements of the original array are not really changed in order. Just the pointer is pointing to the elements in a different order. Do you see what I mean???

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    as biossx said the array will be reversed..... look at the while loop... you are taking the value from the end of the array and swopping it with the value from the start of the array... then you move both 'iterators' towards each other by 1 element. The while condition ensures that the 'iterators' dont cross over with each other.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM