How do I declare a pointer to a pointer to an array? - c++

I have a pointer to an array of Ts:
T (*anArray)[]; // A pointer to an array of Ts.
How do I declare a pointer to a pointer to an array of Ts? (ie. a pointer to the above.)
Is it:
T (**anArray)[];
or
T *(*anArray)[];
or
T (*anArray)*[];
or something else entirely?
Thanks,
Alf

The first one - T(**anArray)[];

The answer to your question is that the extra pointers outside of the parenthesis are applied to the type contained in the array, while inside the parenthesis they are applied to the type of the variable itself:
int (*array)[10]; // pointer to array of 10 int
int *(*array)[10]; // pointer to array of 10 pointer to int
int (**array)[10]; // pointer to pointer to array of 10 int
But the best advice is to avoid the problem and use typedefs:
typedef int array_t[10];
array_t **variable; // pointer to pointer to array of 10 integers

T *(*anArray)[]; -> anArray is pointer to array of T pointers
T (**anArray)[]; -> anArray is pointer to pointer to array of T

Open cdecl.org and then copy-paste the following to the textbox:
declare a as pointer to pointer to array 10 of int
On pasting, it tells you what it means in C++. Well it is this:
int (**a)[10]
I'm sure that is what you're looking for? Play around with the link more. You will learn more syntaxes, and how to say that in words.

A good way of figuring out how to make unusual combinations of pointers like this is as follows: start with the variable name, look right without crossing over parentheses, look left, repeat. Say what you see in the order that you see it.
For a pointer to a pointer to an array we have
int (**a)[5]
Start with a. We look right, but there's a paren there, so look left. Ok we see "*", so it's a pointer. Now we look right again... still a paren, so back to the left. Another "*", so it's a pointer to a pointer. Look right, paren, look left, paren, so now we can jump out of the parentheses. Now we see brackets, so we have a pointer to a pointer to an array. Finally, look all the way to the left and we see int. Pointer to a pointer to an array of ints.

Related

Can I determent typename T* in template is a array or object? [duplicate]

I want to know whether a pointer is pointing to an array or single integer. I have a function which takes two pointer (int and char) as input and tell whether a pointer is pointing to an array or single integer.
pointer=pointer+4;
pointer1=pointer1+4;
Is this a good idea?
Like others have said here, C doesn't know what a pointer is pointing to. However if you should choose to go down this path, you could put a sentinel value in the integer or first position in the array to indicate what it is...
#define ARRAY_SENTINEL -1
int x = 0;
int x_array[3] = {ARRAY_SENTINEL, 7, 11};
pointer = &x_array[0];
if (*pointer == ARRAY_SENTINEL)
{
// do some crazy stuff
}
pointer = &x;
if (*pointer != ARRAY_SENTINEL)
{
// do some more crazy stuff
}
That's not a good idea. Using just raw pointers there's no way to know if they point to an array or a single value.
A pointer that is being used as an array and a pointer to a single values are identical - they're both just a memory address - so theres no information to use to distinguish between them. If you post what you want to ultimately do there might be a solution that doesn't rely on comparing pointers to arrays and single values.
Actually pointers point to a piece of memory, not integers or arrays. It is not possible to distinguish if an integer is single variable or the integer is an element of array, both will look exactly the same in memory.
Can you use some C++ data structures, std::vector for example?
For C++ questions, the answer is simple. Do not use C-style dynamic arrays in C++. Whenever you need a C-style dynamic array, you should use std::vector.
This way you would never guess what the pointer points to, because only std::vector will be holding an array.

C++ char pointer to char variable

I'm given a method header as so:
char* thisMethod(char* input){}
is it possible to say,
char var = input[0];
? I know that "input" will be in the form of a char array.
I'm obviously new to C++ pointers are throwing me off. I tried researching how to work with char pointer function arguments but couldn't find anything specific enough to help. Thanks for the help.
There is a misconception that may lead you into further troubles:
I know that "input" will be in the form of a char array.
NOPE: By the scope of that function, input is a pointer to a character. The function has no way to know where such a pointer comes from.
If it has been taken from an array upon calling the function than it will be a pointer to the first element of that array.
Because pointer have an arithmetic that allows to add offsets and because the [] operator applied to pointers translates as a[b] = *(a+b) by definition, in whatever code, if a is a pointer, *a and a[0] are perfect synonymous.
Think to an array as a sequence of boxes and a pointer as your hand's index finger
adding an offset to a finger (like finger+2) means "re-point it aside" and de-referencing it (like *finger) means "look inside what it points to".
The [] operator on pointers is just a shortcut to do both operations at once.
Arrays are another distinct thing. Don't think to them when dealing with pointers, since -in more complex situations, like multidimensional array or multi-indirection pointers - the expression a[b][c] won't work anymore the same way.
There are two ways to get a value from a pointer, * and []. The following are equivalent:
char var1 = *input;
char var2 = input[0];
Using the brackets is more common when you know you were passed an array, since it allows you to supply an index. You need some way of knowing where the end of the array is so that you don't attempt any access past it, your function is missing that important detail.
As long as it's inside the function and input points to something valid ( not NULL/nullptr and not a garbage location ) then doing char var = input[0]; is just fine. It's the same as char var = *input.
P.S If it's supposed to be a string I recommend using std::string.

How to check if a pointer points to an array or single int or char

I want to know whether a pointer is pointing to an array or single integer. I have a function which takes two pointer (int and char) as input and tell whether a pointer is pointing to an array or single integer.
pointer=pointer+4;
pointer1=pointer1+4;
Is this a good idea?
Like others have said here, C doesn't know what a pointer is pointing to. However if you should choose to go down this path, you could put a sentinel value in the integer or first position in the array to indicate what it is...
#define ARRAY_SENTINEL -1
int x = 0;
int x_array[3] = {ARRAY_SENTINEL, 7, 11};
pointer = &x_array[0];
if (*pointer == ARRAY_SENTINEL)
{
// do some crazy stuff
}
pointer = &x;
if (*pointer != ARRAY_SENTINEL)
{
// do some more crazy stuff
}
That's not a good idea. Using just raw pointers there's no way to know if they point to an array or a single value.
A pointer that is being used as an array and a pointer to a single values are identical - they're both just a memory address - so theres no information to use to distinguish between them. If you post what you want to ultimately do there might be a solution that doesn't rely on comparing pointers to arrays and single values.
Actually pointers point to a piece of memory, not integers or arrays. It is not possible to distinguish if an integer is single variable or the integer is an element of array, both will look exactly the same in memory.
Can you use some C++ data structures, std::vector for example?
For C++ questions, the answer is simple. Do not use C-style dynamic arrays in C++. Whenever you need a C-style dynamic array, you should use std::vector.
This way you would never guess what the pointer points to, because only std::vector will be holding an array.

Pointers to multidimensional arrays [duplicate]

This question already has answers here:
C pointer to array/array of pointers disambiguation
(13 answers)
Closed 7 years ago.
What is the difference between
int *p1[M][N]
and
int (*p2)[M][N]
Also if we define another such pointer
int (*p3)[M][N][K]
what does this represent?
If anyone can explain the differences between the above three, it will be very helpful.
int *p1[M][N] is a 2D array of pointers.
int (*p2)[M][N] is a pointer to a 2D array.
int (*p3)[M][N][K] is a pointer to a 3D array.
It helps to know that you can read any C declaration using an alternating right-and-left fashion. So the direct answer to your question is:
int *p1[M][N] is an array of arrays of pointers-to-ints.
int (*p2)[M][N] is a pointer to an array of arrays of ints.
int (*p3)[M][N][K] is a pointer to an array of arrays of arrays of integers.
You can think of these using the standard "2D" or "3D" terminology, where a 2D array is a table with the first set of brackets indicating the row and the second set of brackets indicating the column. This is not 100% faithful to the way the computer actually works, but it's a highly effective mental model. But going back to how to decipher complicated C declarations:
You read them boustrophedonically, so first right, then left, then right, then left, and so on. Here's an example.
int * const (*p[30])(int x)
we start at the name being declared, p. We look right to get the size of p, which is 30.
So now we know p is an array of 30 something. So we look left, and we find *, so we know p is an array of 30 pointers to something. So we look right, and find the parentheses, indicating a function. Now we know p is an array of pointers to functions. We can trivially include that the parameters to these functions should be an int x.
So we've run out of things on the right, so we just keep interpreting the left elements.
p is an array of pointers to functions returning... const. There's more on the left, so keep going. p is an array of pointers to functions returning const pointers to int.

In C++, What's the difference between char array's name and a char pointer? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is array name a pointer in C?
char arr[1024];
arr++ //move arr to arr+1, error!
I have heard the the name of a char array is a char pointer, is it?
The name of an array decays to an pointer to its first element sometimes.
An expression with array type will convert to a pointer anytime an array type is not legal, but a pointer type is.
You cannot do:
arr++;
Because array is an non modifiable l-value.
An array is a block of memory to which you gave a name.
What does it mean to "increment" it by one? That doesn't make any sense.
A pointer is a memory address. "Incrementing" it by one means to make it point to the element after it.
Hope that makes sense.
Arrays and pointers are not always exchangeable. A more interesting example to illustrate the difference between arrays and pointers is a 2D array:
Consider int **a and int b[3][3].
In the first approach to a 2D array we have a 1D array of pointers to 1D arrays in memory (of course you have to allocate the memory dynamically to use this).
In the second approach of actually using a 2D C array, we have the elements laid out sequentially in memory, and there's no separate location where an array of pointers are stored.
If you try to dereference b, you get a pointer to its first element (i.e. b gets converted to an int (*)[3] type).
nothing, apart from looks..
Well and as you defined the array there you declared already 1024 bytes for the array. Also you obviously can't change the array "base".
An array name is for most (but not all) purposes identical to a constant pointer (not to be confused with a pointer to a constant). Because it's a constant, you cannot modify it with the increment operator ++. There's a good answer explaining this in more detail in an older similar question.