Thread: Referencing an array using s pointer?

1. 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. Err... just get a pointer to point to the element at index 50? Or do you mean find the element with value 50?

3. To find the actual number 50. And I would like to do it recursively, starting at 0.

4. 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.

5. *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. 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.

7. 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. There is not much to do: when passed as an argument, an array is converted to a pointer to its first element.

9. Ok, thanks for the help. I think I should be set.

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

11. 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. 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 .