By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Operator [ ]

P: n/a
Until recently I grasp the relationship between pointers and arrays and
I have finally understand that [ ] is an operator. But I still can't
quite figure it out: What does really does [ ] in a statement such as:

int array[5];
int matrix[5][5];
dArray = new int[];
dMatrix = new int*[];

Also, is this valid (useful?):

int x;
int *array=&x;
for(int i=0; i<some_number; ++i)
array[i] = //some stuff

Oct 31 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Gaijinco wrote:
Until recently I grasp the relationship between pointers and arrays and
I have finally understand that [ ] is an operator. But I still can't
quite figure it out: What does really does [ ] in a statement such as:

int array[5];
int matrix[5][5];
dArray = new int[];
dMatrix = new int*[];
The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.
Also, is this valid (useful?):

int x;
int *array=&x;
for(int i=0; i<some_number; ++i)
array[i] = //some stuff


No, it's not valid for values of 'i' other than 0 (it has undefined
behaviour), and therefore not useful in normal programming.

V
Oct 31 '05 #2

P: n/a
> The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.


I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?

So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?

And if that's the way it works then what it means:

Then

int array[5]

is the same as

int* array = new int[5]

?

Oct 31 '05 #3

P: n/a
> The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.


I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?

So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?

And if that's the way it works then

int array[5]

is the same as

int* array = new int[5]

?

Oct 31 '05 #4

P: n/a
Gaijinco wrote:
The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.
I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?


No, the program statically allocates an array of 5 ints, either on the
stack or in its global memory, depending on the scope in which this
declaration appears. There is no dynamic memory allocation invoked by
this declaration.
So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?
This statement statically allocates an array that contains 5 arrays
exactly like the array in first declaration.

N And if that's the way it works then

int array[5]

is the same as

int* array = new int[5]

?


The "array" variables are interchangeable since an array can be
accessed via a pointer and a pointer can be subscripted, but
technically they are not the same. An array declaration reserves memory
for its elements while a pointer declaration does not reserve memory
for the item or items it points to.

Greg

Oct 31 '05 #5

P: n/a
(a) Don't post twice within minutes: this is not a chat room.

Gaijinco wrote:
The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.

I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?


False.

You're saying, 'array' is the name that will in my program identify
an array of 5 objects, each of type 'int'. Whether (and where) the
memory is allocated, is irrelevant. And 'array' is not a pointer.
So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?
It is, again, irrelevant how it looks and for how many "blocks". What
you say by this declaration is that 'matrix' is the _identifier_ that
is associated with an array of 5 arrays, and each of them is of 5 ints.
And if that's the way it works
It's not.
then

int array[5]

is the same as

int* array = new int[5]

?


Of course not.

V
Oct 31 '05 #6

P: n/a
TIT
Gaijinco sade:
The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.

I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?


No, you declare an array of 5 integers.

int array[5] = {1,2,3,4,5};
So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?


You declare an array with 5 arrays of 5 integers.

int matrix[5][5] =
{
{1,2,3,4,5}, // matrix[0][x]
{1,2,3,4,5}, // matrix[1][x]
{1,2,3,4,5}, // matrix[2][x]
{1,2,3,4,5}, // matrix[3][x]
{1,2,3,4,5} // matrix[4][x]
};

TIT
Oct 31 '05 #7

P: n/a

And if that's the way it works then what it means:

Then

int array[5]

is the same as

int* array = new int[5]

?


True, but with a subtle difference: doing a sizeof(array) will return 5 *
sizeof(int) whereas the last one will return sizeof(int *)

-M
Oct 31 '05 #8

P: n/a
Gaijinco wrote:
The brackets here are part of the syntax. However, it the presented form
it isn't going to compile, there has to be an integral expression inside
the brackets.

I'm a little lost!

When I use something like:

int array[5];

I'm saying "look for 5 block of free memory to allocate int's and
create a pointer "array" to the first block". True or false?


Not really, in fact almost completely wrong. '5' is correct and 'int' is
correct but nothing else is. There is no looking, there is no allocating
and there is no pointer.

The above is a declaration which creates an array (helpfully called
array) of 5 integers. There's nothing more to be said, you are making it
more complex than it is.

So when I'm saying

int matrix[5][5];

It looks for 25 blocks instead of 5, but I cn't understand what [5][5]
really means?

25 is right, I guess you are asking for the difference between

int matrix[5][5];

and

int array[25];

Both create arrays of 25 integers. The differnce in in the way they are
accessed. To get at an individual int in 'matrix' you must subscript
twice (e.g. matrix[1][2]) whereas with 'array' you only need to
subscript once (e.g. array[12]). I think you probably know this and are
again seeing compications where none exist.
And if that's the way it works then

int array[5]

is the same as

int* array = new int[5]

?


No, different. In that case array is a pointer to a block of memory
large engough to hold 5 integers. Here all the stuff about looking and
allocating really does happen.

The similarity bettwen pointers and array happens in the way they are
accessed not the way they are created.

int* ptr = new int[5];
int array[5];

The creation of ptr and array are completely different. But once they
have been created they way there are accessed is very similar. For instance

ptr[1] = 2;

and

array[1] = 2;

are both legal. There still are differences, for instance

sizeof ptr != sizeof array

and

ptr = array;

is legal but

array = ptr;

is not.

john
Oct 31 '05 #9

P: n/a
OK, lot's of intresting things here!

But I don't understand why if:

int a[5];
int* b = new int[5];

Then why

b = a;

is valid but

a = b;

isn't?

Also if this works:

int a[5];

for(unsigned i=0; i<5; ++i)
*(a+i)=i;

It should prove that in the first line we really define a pointer named
"a" because the unary operator * is only for pointers. Am I wrong?

P.D: I didn't post twice within minutes but I tried to edit the post, I
couldn't figure why so I delete and repost edited later.

Nov 1 '05 #10

P: n/a
Gaijinco wrote:
OK, lot's of intresting things here!

But I don't understand why if:

int a[5];
int* b = new int[5];

Then why

b = a;

is valid but

a = b;

isn't?
Because you can assign another value to a pointer but arrays are not
assignable. Language limitation.
Also if this works:

int a[5];

for(unsigned i=0; i<5; ++i)
*(a+i)=i;

It should prove that in the first line we really define a pointer
named "a" because the unary operator * is only for pointers. Am I
wrong?


No. Using the name of an array in some expressions causes the type
to _decay_ from "an array of T" to "a pointer to T". Additive
expression is just one of those cases. When you write (a+i), 'a'
degrades, decays, to a pointer. Same with a[i] (or i[a]).

V
Nov 1 '05 #11

P: n/a
>
Also if this works:

int a[5];

for(unsigned i=0; i<5; ++i)
*(a+i)=i;

It should prove that in the first line we really define a pointer
named "a" because the unary operator * is only for pointers. Am I
wrong?

No. Using the name of an array in some expressions causes the type
to _decay_ from "an array of T" to "a pointer to T". Additive
expression is just one of those cases. When you write (a+i), 'a'
degrades, decays, to a pointer. Same with a[i] (or i[a]).


This is the essential truth. When books confusingly talk about the
'equivalence' of arrays and pointers they actually mean what Victor has
said above.

Arrays and pointers are different but in many contexts (but not all) you
can use an array as if it were a pointer.

john
Nov 1 '05 #12

P: n/a

Gaijinco wrote:
OK, lot's of intresting things here!

But I don't understand why if:

int a[5];
int* b = new int[5];

Then why

b = a;

is valid but

a = b;

isn't?
In C++ the name of an array can be used as the address of that array.
The expression, b = a, demonstrates this point. Similarly, the name of
a function can be used as the address of that function. But neither
arrays nor functionS are themselves pointers, so the expression, a = b,
which attempts to assign an address to an array, is invalid.

Also if this works:

int a[5];

for(unsigned i=0; i<5; ++i)
*(a+i)=i;

It should prove that in the first line we really define a pointer named
"a" because the unary operator * is only for pointers. Am I wrong?


Again a is being used as the address of the a array, or &a[0], if you
prefer.

Greg

Nov 1 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.