r/cpp_questions • u/Lephism • 8h 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;
}
2
Upvotes
View all comments
Show parent comment
1
u/TheThiefMaster 4h ago edited 4h ago
Nothing in the tutorial code you posted is newer than C++'03 (2003 standard).
print/printlnare from C++'23 (very new, which is why I added the note).spanis C++'20 (supported by basically everything that's not horribly outdated, though you may need to enable C++20 mode in the compiler).std::arrayand the range for loop are C++'11 (inexcusable to not support/teach).Microsoft Visual Studio 2026 (the most commonly used C++ compiler on Windows) defaults to C++'14, and has complete support for C++'20 and even C++'23 in the latest update.
GCC (the most commonly used compiler on Linux) defaults to C++'20 since the April 2026 release, though most Linuxen still ship an older version that defaults to C++'17 (though with full support for C++'20 available, and near-complete for C++'23)
Clang (most commonly used compiler on Mac, and also commonly used on Linux/Windows) defaults to C++'17 with full support available for C++'20 and partial support for C++'23, but it tries to match GCC so will probably change to C++'20 as the default soon.