4

Silly question but how do I make a pointer to an array in C++? My understanding is we have a pointer to the first element of an array, but what if we wanted a pointer to that pointer.

int arr[3] = { 1, 2, 3 };
auto arrp = &arr;

cout << arr << " " << arrp << endl;
cout << typeid(arr).name() << " " << typeid(arrp).name() << endl;

// 0115FC24 0115FC24
// int [3] int (*)[3]

Using the auto keyword/experimenting it seems like it can be done, but I can't type out int (*)[3] arrp = &arr;

Is there a way to type it out?

2
  • 1
    I recommend you look up "The right-left rule". Commented Aug 15, 2021 at 7:45
  • 1
    You may not actually want to do this. In C and C++, arrays decay on use to pointers to their first element. And if you want to have multiple "arrays" of length 3, just allocate a buffer (not on the stack!) of 3*n elements, and point into that. I also suggest reading the C FAQ section about arrays and pointers. Commented Aug 15, 2021 at 7:46

1 Answer 1

6

Pointers to arrays and function pointers have weird syntax.

int (* arrp2)[3] = &arr;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you to everyone, this article explained right-left rule when it comes to function pointers and weird C++ syntax: link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.