How do I dereference a pointer to an array

Pages: 12
I have a pointer to an array and when i try to do: std::cout << *arr[i] << std::endl; but that just crashes the program.

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

void Pointer(int* arr[]);

int main()
{
    int* arr[20];

    Pointer(arr);

	return 0;
}

void Pointer(int* arr[])
{
    for(int i = 0; i < 20; i++)
    {
        std::cout << arr[i] << std::endl;
    }
}
int* arr[20] is actually not a pointer to the array, it is an array having 20 pointers to int.

You probably don't need a pointer to the array at all, so just remove all the '*' and it will be fine.
well im trying to practice my use of pointers so i need it.
Well, you can make a pointer to an array like this:

int (*arr)[20];

But keep in mind, you are creating just a pointer (to an array) there, not the actual array. So, there is nothing to print out.

I would suggest that you use vectors instead of arrays, like:

vector<int> arr(20);
vector<int>* arrP = &arr;

etc...

Can someone explain to me why does every BEGINNER here use C-style arrays? I mean, do they all have time machines and are attending some 70's C programming course? And, no wonder, then they runt into trouble. std::vector should be highly preferred over arrays.
Can someone explain to me why does every BEGINNER here use C-style arrays?


Sure, because you need to know arrays to be a competent c++ programmer. It annoys me when people tell beginners not to learn arrays. Arrays are an important part of c++. Even if you never use them yourself, you will undoubtedly encounter them in older code. I use several libraries that require the use of arrays, not to mention that you will encounter c code at some point.
Sure, arrays are important part of C++, but why do BEGINNERS start with arrays instead of vectors?
Or: why do BEGINNERS learn arrays before vectors?
closed account (E0p9LyTq)
why do BEGINNERS learn arrays before vectors?

Because most of the people teaching couldn't write good code to save their hide, and learned C++ before there were standards most likely.

Most books are just as bad. You can find truckloads written to C++03 (or earlier) standards, but it it rare to find C++11 or later.

Let's forget if the instructors or books actually teach C++ in a way that actually educates people.
Oh, so it's outdated resources thing (and, also, outdated instructors).

Thx for the reply, I didn't know that the problem is so widespread.
Last edited on
closed account (48T7M4Gy)
Because most of the people teaching couldn't write good code to save their hide, and learned C++ before there were standards most likely.


I'm not an academic so I don't have a position to defend. But I think the above comment is drawing a very very long bow.

Even the god of C++, the man himself, devotes substantial time in the introductions to arrays and then later goes on to talk about the limitation and pitfalls along with the niceties of later developments and alternative options.

I always wonder whether the array-police wouldn't be better off sticking with QBasic and not advancing their learning at all.

I think/know there's a difference with a university education and getting your education at a game-cutter school.
Interesting.

Ok, so you are saying (correct me if I'm wrong):

- The people who teach arrays-first actually have a rationalization for it. (of course, I would opine that the ratonalization is flawed, but that is another issue...). I mean, it is not because they are lacking experiecne with vectors, or they are afraid of vectors, or they are used to arrays, or they just don't want to modernize and update their old materials.

I mean, are their reasons at least somewhat legit, or are they just an excuse? Because I can't think of a good reason to support arrays-first. Ok, perhaps if an instructor is a believer in low-level programming and low-level optimizations, then I would believe that he is not just mocking me. (but I would still think that he is a bad instructor).

- Those people are actually in majority, as it seems

- You are undecided on the issue yourself, and you are wondering if it has any substantial impact in pratcice.

I'm also wondering whether, if you were teaching C++, would you decide to go arrays-first or vectors-first?
Last edited on
closed account (48T7M4Gy)
Because I can't think of a good reason to support arrays-first.


Being incapable of thinking is not a strong argument.
Right, but, for example, if you google "C++ teach C-arrays", you stumble upon tons of materials that describe why it is bad.

I mean, where is an article listing arguments supporting arrays-first? I can't seem to find any such thing.

If the arrays-first side doesn't even have arguments, then it is just excuses and laziness.

And worst, those people are in majority, and without arguments, it looks really bad and unconvincing.
Last edited on
closed account (48T7M4Gy)
I can't seem to find any such thing.

That's nearly as poor an argument as not being able to think Kevvy.
I don't think so. If there is an argument, someone would have written about it. Therefore, there is no argument of any significance.

Obviously, we disagree on some very basic philosophical issues.
closed account (48T7M4Gy)
Obviously, we disagree on some very basic philosophical issues.

That's quite right Inspector Kevvy of the Array Police. Go get 'em Tiger!
Right, you are mocking, and that is a kind of rude.

Tell me, what do you think, is a decimal number system better than Roman numerals? And what are the arguments for and against? Are there any arguments for one or the other at all?

And is it irrelevant whether we use Roman or decimal number system? Is world a better place because of good or bad decisions that humanity made?

Edit:especially considering one of your previous comments about bringing students to tears.
Last edited on
closed account (48T7M4Gy)
Are there any arguments for one or the other at all?

If you surf around these threads for a short while Inspector Kevvy, you'll see numerous examples of where C++ exercises address the two systems and the task of converting between the two in great detail.
Red herring. My last comment has nothing to do with C++ or conversion between number systems, neither did I mention those two.

But I'm beginning to wonder whether you are able to read English correctly.
woah, that escilated quickly. Also, who says i'm a beginner? I've been programming for 6 years... I'm just digging deeper into C++ and trying to learn even more. I already know how to use vectors as well.
Last edited on
Pages: 12