473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Operator [ ]

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
12 1312
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
> 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
> 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
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
(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
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

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
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
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
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
>
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.