-1

Hi I am a bit confused with arrays. I have tried searching them up, but I haven't found the answer I want.

Confused about arrays like this :

Item **queue;

Does that mean item is creating a 2d array for queue? And what would the array look like? Please give me a brief explanation covering all this. Thank you

  • 2
    That is not an array, that is a pointer to a pointer to an Item. – Elliott Frisch Apr 02 '16 at 15:51
  • From what I heard. 2 pointers are a 2d array. One meaning a pointer to queue and the other meaning an array – user6091183 Apr 02 '16 at 15:53
  • 2d arrays are declared as `que[10][20]` -- but you cannot use them in connection with a pointer to a pointer. – Soren Apr 02 '16 at 16:00
  • Possible duplicate of [How do I declare a 2d array in C++ using new?](http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new) – Soren Apr 02 '16 at 16:01
  • bookmark this site: http://en.cppreference.com/w/cpp/language/array – Richard Hodges Apr 02 '16 at 16:30
  • @Soren What are you talking about? A duplicate? The questions are totally different. What do you mean? – user6091183 Apr 05 '16 at 04:08
  • @user6091183 -- The questions goes deeper than yours in explaining the difference between `**` and `[][]` and the answers (which I assume you have read) goes deeper explaining the memory layout differences -- so your right, your question is a subset and not strictly a duplicate – Soren Apr 06 '16 at 18:17
  • @Soren I think you have a complete misundertsanding. This is the only question I've asked about arrays. – user6091183 Apr 14 '16 at 04:01

3 Answers3

2
Item **queue;

It is not an array. It is a pointer to a pointer to an Item. You can use it to refer to a memory location that contains an array of pointers to Items.

Does that mean item is creating a 2d array for queue?

No. No array is created when you declare a pointer to a pointer. You just create a pointer which might refer to a 2d array. The creation of the 2d array is up to the developer.

This is an example how you could initialise the queue:

int main()
{
  Item * array_1 = new Item[2]; // array of Items
  Item * array_2 = new Item[2]; // array of Items
  Item * array_3 = new Item[2]; // array of Items
  Item ** queue = new Item*[3]; // array of pointers 

  queue[0] = array_1;
  queue[1] = array_2;
  queue[1] = array_3;

  delete queue[0];
  delete queue[1];
  delete queue[1];
  delete queue;
}

Note, I use the operator new in C++ to create a 2d array in the heap.

Giuseppe Pes
  • 7,772
  • 3
  • 52
  • 90
1

The example you provided

Item **queue;

is not an array, but a pointer-to-a-pointer, however you can assign an address of an array to the pointer (as you can with any pointer).

So somebody creating an array like

Item* queues[100]; // An array of pointer to Item

and you can assign the pointer to a pointer, for example

Item* queues[100]; // An array of pointer to Item
Item **queuepointers;

// .. assume that the queues pointers is allocated by some code here...
// ... then
queuepointers = queues;  
queues[50]->someMethodInItem();
Soren
  • 14,402
  • 4
  • 41
  • 67
0

This is an explanation about variables, pointers and arrays.

In C++, variables are stored in memory. Data in memory have values and addresses. CPUs don't know variable names, they only know memory addresses. When you compile your code, the compiler (actually, the linker) changes the variable name (a.k.a. symbol) to the actual memory address allocated for that variable. So when you retrieve the value of a variable in code, what actually happen is telling CPU the address, and CPU retrieve the data stored in that address of memory.

Addresses are just 32-bit or 64-bit (based on your CPU architecture) unsigned integers. So variables can also contain the addresses of other variables, which is called pointers. Pointers are also variables, the only difference is that pointers are 32-bit or 64-bit unsigned integers whose data is the address of other places.

When you declare a variable like float *a, that means a is a pointer to a variable of type float. So the value of a is not float number, but an unsigned integer. To change the location where a is pointing at, just assign value to a like a = &my_float_var;. Notice that I use & to get the address of variable my_float_var.

As for arrays, they are just multiple variables allocated adjacent to each other. For example, we declare an array int arr[10], and say the address of the first element arr[0] is 100, then the address of second element arr[1] is 104 (an int is 4 bytes, address is measured in bytes). If you put arr without bracket in code, that means the address of the array, i.e. the address of the first element.

You can see that arrays and pointers are similar. You can declare char arrays like:

char str1[] = "some string";
char *str2  = "some string";

Both method is fine. Only str1 is a symbol and you can't alter its value (the location where it's pointing at), but you can change str2's value, making it pointing to another string.

Back to your question, Item **queue can be though as a pointer of type Item *, or an array of type Item *. And type Item * can be pointer of type Item, or array of type Item.

So if queue is an array of type Item *, while each element of that array points to another array of type Item, queue can be thought of a 2d array. But that's different from conventional 2d arrays. Conventional 2d array use syntax of arr[1,2] to access elements, but accessing queue's element should use queue[1][2].

Ziming Song
  • 1,176
  • 1
  • 9
  • 22