OK, since I don't like the other answer here (because I think it's leading you in the wrong direction), I will try to persuade you to use modern C++ idioms instead. As I have said more than once in the comments, this will pay off in spades once you master it.
First up, your question contradicts itself. First you say this:
[...] Each of the resulting pointers would then be assigned to point to an array of 8 unsigned char.
But then you say this:
I think that the variable declaration for my array should be:
int (**arr)[8];
So what do you want? Some sort of array of int
's or an array of arrays of 8 unsigned char
's? I'm going to go with the latter since that is what you said your goal is. If that's not in fact what you want, please clarify your question - concentrating on what data structure it is you want to represent, rather than how you think it should be achieved - and I will update this answer accordingly. Like I said (although I have now deleted the comment because I thought it was a little unkind), XY problem. And please forget about pointers. That's not the way to solve this problem (whatever it is, or any other, pretty much).
So, in modern C++, this is super-easy. For the sake of argument, let's suppose you want a data structure representing a fixed number of arrays (4, say) of unsigned char
s, each array of unsigned char
s being 8 bytes long. Well that's trivial, you can make use of what has to be the simplest STL container - std::array
- and simply say:
// Define your types once ('using' is a sort of supercharged typedef) and use them everywhere
using my_array_element_type = std::array <unsigned char, 8>;
using my_array_type = std::array <my_array_element_type, 4>;
my_array_type my_array { }; // { } zero-initialises the array (I'm playing it safe here)
And then, to access an individual element in the array, you might do:
auto c = my_array [3] [5]; // 'auto' 'deduces' to unsigned char here
Now this looks lot like a 2-dimensional C-style array allocated on the stack, and indeed that's exactly what it is, but there are some important differences.
Given the above you can, for example, do this kind of thing:
my_array_type make_array ()
{
my_array_type a { };
// stuff things into the array, perhaps
return a;
}
and then, at the point you want the array to come into existence, you can do:
auto my_array = make_array ();
And you can also write functions like this:
void use_array_read_only (const my_array_type &a) // passed by reference, so not copied
{
// .. do things with a (read-only)
}
and this:
void use_array_read_write (my_array_type &a) // ditto
{
// .. do things with a (read-write)
}
Note that these functions know all about the type of the parameter being passed to them, including how many elements it contains (so no need to pass additional one-day-you're bound to get-it-wrong parameters for these), and you can't say that about C-style arrays. The fact that these 'decay' to a pointer is notorious, everybody hates it. Even working out the type of the parameter for 2D arrays is a challenge which I personally am not smart enough to figure out unless I absolutely have to.
Being a stack-based variable, the lifetime of my_array
is limited to the scope in which it is defined. I can only guess at whether this is an issue for you, but I'm sure you can cope with that. Also, since they're allocated on the stack, std::array
s must not be too big.
And if you want a data structure which can be resized, or is too large to fit comfortably on the stack, you can use std::vector
instead. std::vector
's API is in many ways a superset of that offered by std::array
, so, for many purposes, it can be used in much the same way, once populated.
Right, tl;dr:
Forget, basically, all the C that you know; it will just lead you astray. In particular, forget that pointers and C-style arrays exist. (For now anyway. Pointers do have their place, but you need to learn about 'smart pointers' to use them safely and that's for another day).
Forget all about malloc
, free
, new
and delete
. You won't need them. Ever.
Learn to make basic use of the three containers that are the workhorses of the STL: std::array
, std::vector
and std::string
. They're not hard to use and if you run into something you don't know how to do or don't understand, you can always ask a question here on SO. That's what the site is for.
Learn what references (and const
references) are. You will want to use them as function parameters to avoid copying a container every time you pass it to a function. It will also allow you to modify that container inside the function, if that's what you want to do.
Buy yourself a suitable book. You need it now, not next week or next month or next year!
Be aware that cppreference exists. You'll find it tough at first but it's a really useful site and things will get easier. Also, when starting out, you might find (cplusplus.com)[https://www.cplusplus.com/] more accessible, but cppreference is more comprehensive and more reliable.
When you want to try out something new, you can use one of the many online compilers that are out there. I find them invaluable and personally favour Wandbox. The link points to a simple demo of std::array
for you which you might like to play with.
That's it. Best of luck! Mine's a beer. If you get stuck in, you'll find a lot of people willing to help you here on SO. If you stick with outdated C idioms, not so much. We see a lot of that here and it's kinda depressing.