Like Tree1Likes
  • 1 Post By std10093

Referencing an array using s pointer?

This is a discussion on Referencing an array using s pointer? within the C Programming forums, part of the General Programming Boards category; Hi, I need to get inside of an array and be able to increment the reference point. Would I need ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    21

    Referencing an array using s pointer?

    Hi, I need to get inside of an array and be able to increment the reference point.

    Would I need some kind of pointer?

    For example: If I have an array of 100, and I need to access 50,
    is there a pointer that I can reference the array with, to start at 0 and implement till it finds 50 or NULL?

    If so how do I declare/initialize it to get inside the array?

    Much thanks! =-)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Err... just get a pointer to point to the element at index 50? Or do you mean find the element with value 50?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    To find the actual number 50. And I would like to do it recursively, starting at 0.
    Last edited by gipper; 11-27-2012 at 09:34 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Please use more accurate language. What is "the element 50"? That does not make sense. There is no such thing as the element 50. With a 100 element array, there is an element at index 50. With an array of integers, there may be an element with the value of 50. But there is no element 50.

    I ask this especially because you say "starting at 0". That makes it sound like you mean index. But if so, then there is nothing to search: x[50] is the element at index 50. No recursion or iteration required.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    *Number 50. Sorry...

    And yes, starting at the INDEX 0, and then incrementing one at a time throughout the array, till I find what I'm looking for, or NULL.

    I just need to know what kind of pointer could do the job.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Ah. In this case, you have a few options. One option is for your recursive function to take a pointer to the first element of the array as an argument, then the index of the current element as another argument. If you want the array length to be flexible, then you take the number of elements of the array as yet another argument.

    Another option is to pass a pointer to the current element as well as a pointer to one past the end of the array. In this approach, your base case is when the pointer to the current element is equal to the one past the end pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Thanks so much for the ideas. They will definitely be put to use. But my biggest problem is that I don't know how to declare and initialize the pointers to the array.

    Yes, I do realize how easy this is, and I kinda feel dumb asking for more than what you have already told me, but I honestly have no clue..

    Again, thanks so much for the help

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    There is not much to do: when passed as an argument, an array is converted to a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Ok, thanks for the help. I think I should be set.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Read the comments please.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int array[10] = { 0,1,2,3,4,5,6,7,8,9};
    
        int* arrayPtr = NULL;
    
        int i;
    
        /* Let's say that I want to access the 5th   */
        /* element of my array, by setting a pointer */
        /* at the first element and then do pointers */
        /* arithmetic                                */
    
        /* Set the pointer to first element */
        arrayPtr = array;
    
        /* Increment the pointer until we reach the 5th */
        /* element. Do it with a loop.
    
        /* Extremely careful with the conditions of the */
        /* loop or you go out of bounds                 */
        for( i = 0 ; i < 4 ; i++)
        {
            arrayPtr++;
        }
    
        /* Not the pointer is set to the 5th element           */
        /* For test let change the value of 5th element to 124 */
    
        *arrayPtr = 124;
    
        /* Print the whole array */
        for( i = 0 ; i < 10 ; i++)
        {
            printf("%d\n",array[i]);
        }
    
        return 0;
    }
    Hope this helps
    gipper likes this.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    21
    Wow, Thanks sooo much! That sums all of it up.

    So where you have "arrayPtr = array;" That will set the pointer to the first element? Wow.. That is way to easy.. Haha
    Thanks again!

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Exactly!

    Notice that this
    Code:
    arrayPtr = array;
    is equivalent to this
    Code:
    arrayPtr = &array[0];
    Why ?

    Because we are asking for the address ( & ) of the first element ( [0] ) of the array.

    Now I think you can see that you can set easily the pointer to every element of the array you desire by adjusting the index .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Referencing pointer inside a structure pointer
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 11-11-2010, 10:33 AM
  2. Need help for pointer referencing
    By firebird in forum C Programming
    Replies: 1
    Last Post: 10-05-2010, 10:40 PM
  3. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  4. referencing FILE pointer inside struct
    By Cquest in forum C Programming
    Replies: 3
    Last Post: 03-22-2008, 11:53 AM
  5. Referencing part of an array.
    By Russell in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 03:28 PM

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