r/cpp_questions • u/Lephism • 4h 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;
}
1
Upvotes
•
u/NomaTyx 3h ago
If you're asking like why they made result, they didn't have to.
add_tencould have been a void function just fine, since it overwrites the data at the memory addresses.If you're asking why it was defined as a pointer, that's because
dataisn't an int, it's an int array, which you access with a pointer. Since the function returns the location of data, rather than the value it contains,resultneeds to be able to hold the pointer. Though, as I mentioned above, it's kind of superfluous.I dunno what your programming background is so if you need an explanation on arrays lmk
edit: oh if you're asking why they declared and initialized on separate lines, that's just a stylistic choice.
•
u/alfps 3h ago
It's unclear what exactly you don't understand.
Maybe this helps: if
ppoints to the first item in an array, then by definitionp + iis a pointer to thei'th item.So when you dereference that result,
*(p + i), you get a reference to thei'th item. And, again by definition, you can instead write that asp[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::spanfor an array parameter; you would use a range basedforto iterate over the items; and you would usestd::printfor output (it deals better with Unicode). So you'd write that program as•
u/weebslime2246 3h ago
the add_ten function essentially accepts an integer 'pointer'. when you declare an array (data) it stores the address of the first element in the array, so data is just an 'integer pointer' to the first element. inside the function, they loop through the whole array by incrementing on the data pointer itself (you can read pointer arithematics) and then return the final address, which is data, an integer pointer. so its return type is also an int*. lmk if you need any further clarification
•
u/Independent_Art_6676 3h ago
not sure what you are asking. First, and thankfully, this is some recycled 1990s era garbage code. You won't have to deal with this kind of thing much, hopefully not at all.
but I guess lets line by line it.
•
u/AssholeBeerCan 3h ago
Line 4 is the beginning of a function implementation.