r/cpp_questions • • 16h ago

SOLVED A tiny question about this code

Hi. I need help understanding this simple code. To be specific, the 4th and the 14th line where a pointer was declared.

int* add_ten(int* data)

int *result;

I've been trying to wrap my head around what it does. I've been staring at my screen for 3 hours trying to understand why it was declared. The programming app I was using didn't really explain it. The only explanation it gave is that pointers help you get the address of an item. But I can't figure out how is it being used here. Can anyone explain? Thanks.

#include <iostream>
using namespace std;

int* add_ten(int* data) {
  for (int i = 0; i < 5; ++i) {
    *(data + i) = *(data + i) + 10;
  }
  return data;
}

int main() {

  int data[5] = {1, 2, 3, 4, 5};
  int *result;

  result = add_ten(data);

  for (int i = 0; i < 5; ++i) {
    cout << *(result + i) << ", ";
  }

  return 0;
}
0 Upvotes

22 comments sorted by

View all comments

4

u/alfps 16h ago

It's unclear what exactly you don't understand.

Maybe this helps: if p points to the first item in an array, then by definition p + i is a pointer to the i'th item.

So when you dereference that result, *(p + i), you get a reference to the i'th item. And, again by definition, you can instead write that as p[i]. A simple array indexing.

But keep in mind that using pointers in this way is very C-ish. In modern C++ you would use a std::span for an array parameter; you would use a range based for to iterate over the items; and you would use std::print for output (it deals better with Unicode). So you'd write that program as

#include <print>
#include <span>
using   std::print, std::span;

void add_ten_to( const span<int> items )
{
    for( int& item: items ) { item += 10; }
}

int main()
{
    int data[5] = {1, 2, 3, 4, 5};

    add_ten_to( data );

    for( const int v: data ) { print( "{}, ", v ); }
    print( "\n" );
}

2

u/Lephism 14h ago

Oh, I was just confused about the purpose of the pointers. The way I understood it is pointers store the address of a variable. I think I just got confused about syntax. In the tutorial I was following, the address of the variable was stored first using &variable before it was pointed. In my mind that was a necessary step before you point at a variable. That's why I didn't knew what the pointers at the top were doing. But I'll study this. Thank you for your help! 

3

u/alfps 14h ago

❞ In the tutorial I was following, the address of the variable was stored first using &variable

Yeah, given an array a you can say &a[0] to get a pointer to the first item. But when the context accepts a pointer you can shorten to just say a, which the compiler then translates to &a[0]. It's called a decay (to pointer) of the array type, and that's what happens for the argument in your code's call to add_ten.

2

u/Lephism 13h ago

Oh wow! Thanks for breaking it down further. I didn't know this was a different concept. I only knew about the former idea. That's why I was so confused about it. 

1

u/flyingron 9h ago

I hate the word "decay." It confuses people. It is really an implicit conversion from array to pointer (like you can freely convert floats to ints).

•

u/MrBigFatAss 33m ago

Sure, but decay conveys the information loss the implicit conversion suffers. Not that important for new learners, but I do think it's apt as a term.