What does *ArrayName do here?

I am following a tutorial on the internet and the code of the tutorial is bellow.
What does *ArrayName do here? And why has it a pointer (and I don't see a Reference)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

template <class TypeName>
void Proceed(TypeName *ArrayName);

int main(){
    char String1[]= "String";
    int NumArray1[]={1,2,3,4,5,6};
    Proceed(String1);
    Proceed(NumArray1);
    Proceed("Test");
}

template <class TypeName>
void Proceed(TypeName *ArrayName){
    for(int i=0; i<6; i++){
        cout << *ArrayName << endl;
        ++ArrayName;
    }
    cout<< endl;
}


Last edited on
cout << *ArrayName << endl;

Here, ArrayName is a pointer. * here means "the thing this pointer is pointing at".
it is identical to ArrayName[0] if you prefer that notation

which means that your loop would look like this:

for(int i=0; i<6; i++){
cout << ArrayName[i] << endl; //don't need arrayname++ stuff

templates and such indicate a desire to reuse the code, but that hard coded 6 is going to be trouble someday. pass the 6 in as a 'size' variable maybe (better, use vector).

Proceed(NumArray1); //passes in a pointer, ok
Proceed("Test"); //I think this may be illegal in standard c++. Also test0 … you will iterate into at least one garbage location even if it compiles, because test0 is only 5 and that hard coded 6... it could also crash here, or it can run just fine as it does in the shell compiler in the forum. But its risky.
Last edited on
Also, as a function parameter,
void foo(T* arr) { }
is equivalent to
void foo(T arr[]) { }
Topic archived. No new replies allowed.