C++ Pointer and 2d array outputting
I'm new to C++ and still really confused about how 2d arrays work with
pointers. If I have this (in example format):
int* anarray = anarrayfiller();
for (int a=0;a<10;a++) {
for (int b=0;b<10;b++) {
cout<<(char)anarray[a][b]; //Here's the error mentioned below
}
cout<<"\n";
}
//Later, outside main
int* anarrayfiller() {
int anarray[10][10];
//Populated here
return &anarray;
}
This produces an error under b in the cout<< line: "Expression must have
pointer-to-object type" I would just check how to search through 2d
arrays, and I found this: A pointer to 2d array Which suggests that
actually this pointer points to the array of ints inside anarray[0], and
if that's the case, I must be missing something in terms of returning
pointers - wouldn't I then have to return a pointer to a 2d array of
pointers that each points to a specific int from anarray? I'm pretty
confused here. How do pointers work with 2d arrays?
No comments:
Post a Comment