You are using a version without Ads of this website. Please, consider donating:

### array and pointers

Hi All,

Im really confused about array of pointer...

please take a look at the following snippest :

 ``12345`` `````` int a[] = {3,4,5}; int * b = a; ``````

and when I print the array b it prints 3,4,5 as expected

now if I write the following :

 ``12345`` `````` int a[] = {3,4,5}; int * b = {a}; ``````

also prints 3,4,5. I don't understand why? after all doesn't it mean that now b is array of pointers?

In the second example, you declare b as pointer to int. I think you intended it to be pointer to pointer to int:
`int **b = /**/;`
 also prints 3,4,5. I don't understand why? after all doesn't it mean that now b is array of pointers?

No.

`int* b[] = {a};`

Now, b is an array of type pointer to int.
so what is the meaning of b when b declared as follows:

 ``12345`` `````` int a[] = {3,4,5}; int * b = {a} ``````

is b just array of int?
No. The braces during the initialisation of "b" are used to initialise scalar types. These braces are almost never used except during array initialisations. When compiling with GCC, GCC will generate a warning regarding the use of braces unless it's during the initialisation of an array.

 `` `` ``int A_ = { 10 }; // Scalar initialisation. Generates a warning with GCC. ``

In regards to your question, no, "b" will point to the address of the first element of "a".

Wazzak
Last edited on
Thank u !!
nope, b is the first element in the array, the brackets are doing nothing by the way, you can point to a position in the array

 ``12345`` `````` int array[5] ={1,2,12,4,4}; int *arraypointer = array [2] ; cout<< *arraypointer; //outputs '12' ``````

I remember trying to bend my head around pointers a few months ago, you know how to use them allready you just need to get used to them, if i had spent a few hours just pointing to things and popping them in arguments i would have learnt the faster than reading and asking about them.

For me it clicked when i realized this was possible:

 ``12345678910`` `````` int Number = 10; int *NumberPointer = &Number; int **NumberPointerPointer = &NumberPointer; int ***NumberPointerPointerPointer = &NumberPointerPointer; ``````

As pointless as that is XD, thas when i realized that int *something is making a thing that points.

So i automatically clicked for all things that same moment.

Try to think of the arrays address in memory being pointed to, how you gonna refere to every part of the infomation in that address?

I think you might be better off refering to the address of an array rather than trying to point to a whole array, then when you advance theres vectors, you can point to a vector, its just like a more modern array :)
Last edited on
 ``1234`` ``````int array[5] ={1,2,12,4,4}; int *arraypointer = array [2] ; cout<< *arraypointer; //outputs '12' ``````

Presumably you meant

 ``1234`` ``````int array[5] ={1,2,12,4,4}; int *arraypointer = array+2 ; // or: &array[2] cout<< *arraypointer; //outputs '12' ``````

oops yeah i did, no wait no that tottlay works on my ide oh yeah forgot the ampersand.

To the OP this array+2 is the secret to learning the pointer thing completley, the +2 is arrays position in memory + 2 (the location of index 2!!)
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: