What is the array of pointer to the structure?
is it something like this:
StructureName objectname[size];
int *ptr;
ptr=objectname;
Please confirm me with this.
This is the array of pointers to some structure:
StructureName* objectname[size];
// ^
which is the closest thing I can think of when you refer to:
array of pointer to the structure
I'm assuming you meant ptr to be a StructureName*, otherwise your code won't compile.
No, that is not an array of pointers to a struct. What you have done is used array-to-pointer to conversion to assign the array objectname to ptr. This makes ptr a pointer to the first element of the array.
An array of pointers to struct is something like:
StructureName* objectname[size];
Here, we are declaring an array objectname that has size pointers to StructureNames. The elements of the array are pointers. This makes it an array of pointers.
On the other hand, your title asks about a pointer to an array of structs, in which case it would look like this:
StructureName (*objectName)[size];
Related
Suppose we have that the following 2-dimensional array:
int multi[3][3];
As I understand it, we can then say that multi is a "pointer to a pointer", or -- at the very least -- we can do something like the followiong:
int **p_p_int = multi;
Question: Why don't we say that multi is a "pointer to a pointer to a pointer to pointer", since multi points towards the address of its first element, which itself points to a pointer, which points towards the address of multi[0][0], which itself points to the value of multi[0][0].
This seems to be 4 objects that point (here I'm counting addresses as pointers).
Now you might say "but addresses aren't pointers, even though pointers can equal addresses", in which case it also seems weird to say that a 2-dimensional array is a "pointer to a pointer", since it's actually a pointer to an address to a pointer to an address (to a value).
I feel like I have mananaged to confuse myself quite a bit here :)
A 1d array converts to a pointer rather than being one — e.g. you can reassign a pointer, you cannot reassign an array.
A literal 2d array isn't stored in the way described below.
However a 2d array can be achieved by a pointer to a pointer if:
int **array2d is a pointer. It points to an array. That array contains pointers.
Because array2d points to an array of pointers, array2d[n] is also a pointer. But it's a pointer to an array of integers.
So that's two pointers, total.
In pseudo code, the steps to look up the item at (m, n) are:
add m to array2d to index that array. Read pointer, p from calculated address;
add n to p to index that array. Read integer from calculated address.
The job of a pointer is to point towards something. Just because something is both a pointer and points to something else doesn't make it a pointer to a pointer, unless that something else is a pointer.
So let's break this down:
multi points towards the address of its first element,
That makes it a pointer
which itself points to a pointer,
That makes it a pointer to a pointer.
which points towards the address of multi[0][0],
No. It contains the address of multi[0][0], it doesn't point to it.
which itself points to the value of multi[0][0]
Well, of course. It's a pointer, pointers point to values, that's their job. That doesn't make them pointers to pointers, it makes them pointers to values.
This seems to be 4 objects that point (here I'm counting addresses as pointers).
Sure, but two of those pointings are the very same pointing just counted twice. You say X is a pointer that contains a value that points to something as if that was two separate pointings. The job of a pointer is to have a value that points to something, that's what makes it a pointer in the first place. It's two ways of saying the same thing, "X is a pointer" = "X contains a value (of a type) that points to something".
Saying that multi is a pointer to a pointer is just wrong. It is a 2D array.
multi can not be converted to a ** - only to *
A pointer to pointer would obviously point to a pointer. multi is not doing that. You'll find an integer at that location - not a pointer - simply because multi is not an array of pointers. multi[n] may also be converted to a * but the converted value is not taken from a place where it was stored - it is just calculated from multi.
Don't think of a 2D array like:
int a[3];
int b[3];
int c[3];
int* x[3] = {a, b, c};
cause that is simply not how 2D arrays work.
All pointer values you get from a 2D array are calculated values - not stored values.
I think that there are two unrelated stuff here that you are mixing up:
the name of any array of the type T[][][]...n...[] decayes into T*[][][]...n-1...[] for example:
int arr [7] decayes into int* arr
std::fstream arr[2][2] decays into std::fstream* arr[2]
by decaying, we mean that a function which can get T can also accept T' which T' is the decayed type of T
you can dynamically create psuedo 2 dimentional arrays by using pointer to pointer and dynamic allcoation
int** psuedeo2DimArray = new int*[10];
for (auto i=0U;i<10;i++){
psuedeo2DimArray[i] = new int[10];
}
In all cases, the type of multi is int[2][2] and nothing else.
template <class T>
void swap(T& a, T& b){
T tmp = a; a = b; b = tmp;
}
I am reading a book and it tells me that the code above will not work for arrays unless we overload the '=' operator. I don't understand why it shouldn't work though. Are we not switching around the pointers to the first index of the arrays?
First of all, if you pass arrays as arguments the type T will be deduced to be an array type, and that leads to problem as you can not assign an array only copy them.
Then your misconception that you can just switch pointers around, that can't be done. If you have two pointers, swapping them will work fine but you can't just switch around arrays like that. And it won't be possible to use pointers to the arrays using the address-of operator directly in the call either, as that will attempt to bind references to temporary values which are not possible.
The only solution to "swap arrays" using your function is something like
char array1[...] = { ... };
char array2[...] = { ... };
char* pointer_to_array1 = array1;
char* pointer_to_array2 = array2;
swap(pointer_to_array1, pointer_to_array2);
// After call, pointer_to_array1 will point to the first element of array2
// and pointer_to_array2 will point to the first element of array1
Are we not switching around the pointers to the first index of the arrays?
There is no such thing. It sounds like your mental model of a C-style array is a pointer that points to some elements. However this is wrong. In fact the C-style array is just the elements, there is no pointer.
We can form a temporary pointer to the first element when we need one but there is not such a pointer stored along with the array storage.
Like any other object, an array lives at the same address for its entire lifetime. Swapping two arrays can only be done by swapping the contents of every element.
I'm reading a tutorial on C++ that demonstrates how to call an object's public methods from an element of an array using the subscript syntax. It goes something like this:
Foo* array = new Foo[2];
array->public_function();
array[1].public_function();
Now I understand that the variable array points to the first element of the array and thus uses the -> operator to access that element's public data and functions. My question is, why would the call to the array[1] be any different - why would it use the . syntax as opposed to the -> syntax? Aren't all elements of the array pointers?
Once you use the indexing [], you get the object, not the pointer to the object.
Consider the following example, which works with the pointer directly by using pointer arithmetic.
Foo* array = new Foo[2];
// Following four are equivalent.
array[0].public_function();
array->public_function();
(array + 0)->public_function();
(&array[0])->public_function();
// Following three are equivalent.
array[1].public_function();
(array + 1)->public_function();
(&array[1])->public_function();
You can think of it as if the [] operator returns a reference to Foo, so you use the . operator to access its members.
array->public_function() is the same as (*array).public_function() which is the same as array[0].public_function(). You are accessing public_function() through a raw pointer to a Foo object instance. The compiler has no concept that the pointer is actually an array of Foo instances. Like you said, array is just a pointer to the first element in the array. All of the above syntaxes are valid on any object pointer. Since you are dealing with an array, you should stick with the array[0].public_function() syntax for consistency and readibility.
I'm having trouble with c++ pointers. I have a pointer to array of pointers.
and the case is:
MyType *(*arr)[5];
MyType **a = arr;
the 2nd line gives an error. How can i take pointer to this array ?
The error says it all (include it in your question the next time).
You declare a pointer to array of five pointers to MyType. Then you try to assign it to pointer-to-pointer-to MyType. This is exactly what error message says:
IntelliSense: a value of type "MyType *(*)[5]" cannot be used to initialize an entity of type "int **"
Another suggestion is to tell, what you are trying to achieve. Though my explanation is technically correct, I doubt it will solve your actual problem.
int* arr[8]; // An array of int pointers.
int (*arr)[8]; // A pointer to an array of integers
Here arr is a pointer which points to an array of pointers. So if you want to take pointer to the array, use MyType **a=*(arr+i) where *(arr+i) is the i th array.
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.