2

I am learning how pointers work in C++, and am trying to iterate through an array using pointers and that confusing pointer arithmetic stuff.

#include <iostream>

int main()
{
    float arr[5] = {1.0, 2.0, 3.5, 3.45, 7.95};
    float *ptr1 = arr;
    for (int i = 0; i < 5; *ptr1 + 1)
    {
        std::cout << *ptr1 << std::endl;
    }
}

I declare an array of type float called arr[5]. Then I initialize a pointer variable *ptr1 holding the memory address of arr. I try to iterate it using *ptr1+1 (which gives no error), but then when I do std::cout << *ptr1 + 1 << std::endl I get an error:

operator of * must be a pointer but is of type float

Please help me fix this.

4
  • 6
    I don't see that error with this code. What I do see is that your loop will never end since you never increment i; Commented Sep 27, 2021 at 23:17
  • Cannot reproduce: godbolt.org/z/f5a6v4Txa please provide a proper minimal reproducible example Commented Sep 27, 2021 at 23:17
  • @jkb turns out the error is that I'm just incrementing the value at ptr1's location and NOT the actual index Commented Sep 27, 2021 at 23:31
  • 1
    @ShahJacob "I declare an array of type float called arr[5]" - you are declaring an array of type float[5] named arr. "I initialize a pointer variable *ptr1 holding the memory address of arr" - you are declaring a pointer of type float* named ptr1, and initializing it with the address of the 1st element of arr, due to array-to-pointer decay. "I try to iterate it using *ptr1+1" - that doesn't iterate anything. "when I do std::cout << *ptr1 + 1 << std::endl I get an error" - that statement does not produce that error. Commented Sep 27, 2021 at 23:37

2 Answers 2

3

In your loop, after each iteration, *ptr1 + 1 is dereferencing ptr1 to read the float it is currently pointing at, adding 1 to that value, and then discarding the result. You are not incrementing ptr1 itself by 1 to move to the next float in the array. You likely meant to use ptr1 += 1 instead of *ptr1 + 1.

More importantly, you are not incrementing i, so the loop will not terminate after 5 iterations. It will run forever.

The rest of the code is fine (though your terminology describing it needs some work).

Try this:

#include <iostream>
     
int main()
{
    float arr[5] = {1.0, 2.0, 3.5, 3.45, 7.95};
    float *ptr1 = arr;
    for (int i = 0; i < 5; ++i, ++ptr1)
    {
        std::cout << *ptr1 << std::endl;
    }
}

Online Demo

Though, a simpler way to write this would be to not use manual pointer arithmetic at all, just use normal array indexing notation instead:

#include <iostream>
     
int main()
{
    float arr[5] = {1.0, 2.0, 3.5, 3.45, 7.95};
    for (int i = 0; i < 5; ++i)
    {
        std::cout << arr[i] << std::endl;
    }
}

Online Demo

Or better, use a range-based for loop instead, and let the compiler do all the work for you:

#include <iostream>
     
int main()
{
    float arr[5] = {1.0, 2.0, 3.5, 3.45, 7.95};
    for (float f : arr)
    {
        std::cout << f << std::endl;
    }
}

Online Demo

Sign up to request clarification or add additional context in comments.

15 Comments

@JuanR see my updated answer, I already mentioned that
@RemyLebeau a little confused. what do you mean i'm assigning 1 to ptr1? i'm not assigning / using an equals sign anywhere. also why are you using both ++i AND ++ptr1
Your code worked, I marked it as the corrrect answer. However I don't understand why you're using two update statements and why ptr1 + 1 works and not *ptr1 + 1
@ShahJacob I reworded my answer.
@ShahJacob "what does *(arr+i) to? make use of pointer decay and get the value of the first element in the array?" - the i'th element, not the 1st element. arr decays into a pointer to the 1st element, then that pointer is incremented by i elements, and then the pointer is dereferenced. "adding 1 to arr to move through the elements in the array and then * gets the value of it?" - I assume you meant i not 1, but yes, that is exactly what is happening.
|
2

You are actually adding 1 to the first float in the array but never increasing the pointer. And you don't increment the loop counter, therefore your program will run forever.

You need to correctly increase the pointer and also increase i:

#include <iostream>

int main()
{
    float arr[5] = {1.0, 2.0, 3.5, 3.45, 7.95};
    float *ptr1 = arr;
    for (int i = 0; i < 5; ++i, ++ptr1)
    {
        std::cout << *ptr1 << std::endl;
    }
}

Proof:

https://replit.com/@ichramm/PortlyMiserableNetframework#main.cpp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.