It is just not clicking in my head (Output an array via pointer in reverse)

This is a discussion on It is just not clicking in my head (Output an array via pointer in reverse) within the C++ Programming forums, part of the General Programming Boards category; So this is one of thoose things that is not clicking in my head. I am trying to write a ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    It is just not clicking in my head (Output an array via pointer in reverse)

    So this is one of thoose things that is not clicking in my head.

    I am trying to write a function using "pointer notation" that will write out the elements of an array of int in reverse order. I know this has to be done via a for loop but I am a little lost when it comes to the "pointer notation". When assinging the address of a pointer to an array it assigns it to the first element [0] correct? Anyways I would appreciate it if someone could offer up and example.

    Thanks

    Chad

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    for ( p = &array[n-1] ; p >= array ; p-- )
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Salem
    for ( p = &array[n-1] ; p >= array ; p-- )
    What are the declarations in this? Is p the pointer?

    So p = the address of the array, correct?

    What is n? The element of the array?

    Why n - 1? This has something to do with the fact that arrays actually start at 0 correct?


    Thanks

    Chad

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    So p = the address of the array, correct?
    p is a pointer to an element of the array. If you have T array[n] then it's T *p
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why n - 1? This has something to do with the fact that arrays actually start at 0 correct?
    Exactly:
    Code:
    int a[10];
    /* printf("%i\n", a[10]); /* out of bounds */
    printf("%i\n", a[9]); /* 9 is the last element . . . n (10) - 1 */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What are the declarations in this?
    They were left out for brevity.

    n=size of the array. If you have the array:
    Code:
    int myArray[] = {10, 20, 30};
    the size of the array is 3 but the valid index numbers are:

    myArray[0] = 10
    myArray[1] = 20
    myArray[2] = 30

    An array name is like a pointer to the first element. So, you can set a pointer to the first element like this:
    Code:
    int* p = myArray;
    cout<<*p<<endl; //10
    Or, you can set the pointer to point to other elements of the array:
    Code:
    p = &myArray[2]; //"p is equal to the address of myArray[2]"
    cout<<*p<<endl; //30
    Pointer arithmetic allows you to move the pointer along the array with the increment operator(++) or the decrement operator(--). Since you want to output the elements in reverse order, setting a pointer to the end of the array, and then decrementing that pointer everytime through a for-loop to move it along the array seems like a good approach.
    Last edited by 7stud; 10-21-2005 at 02:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  4. Pros pls help, Pointer to array
    By kokopo2 in forum C Programming
    Replies: 7
    Last Post: 08-17-2005, 11:07 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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