# Thread: the subscript operator

1. ## the subscript operator

Hello,
So, I didnt know I could do this:
Code:
```int main()
{
const int ARRAYSIZE = 10;
int myArray[ARRAYSIZE];
for (int i = 0; i < ARRAYSIZE; i++)
myArray[i] = i+5;
cout << myArray << endl;

int* pArray = myArray;
cout << pArray[0] << endl;
return 0;
}```
So then this means that when you use the subscript operator ( [ ] <- I believe subscript op is what it's called...correct me if I'm wrong), so when you use that operator, the function body automatically dereferences the pointer (in this case pArray)?...so now curiously how would the declaration of an array factor into the use of the [] operator? I mean is it a matter of operator overloading? Just something I was thinking about...if you know of any good text to read on the matter that'll work, dont feel likeyou haveta answer this question yourself..I just like to know the inner workings of whats going on, makes the visualization easier in some cases. Chap

2. FYI, in addition to using array indexing with a pointer variable, you can also use pointer like dereferencing and pointer arithmetic on arrays:

Code:
```const int ARRAYSIZE = 10;
int myArray[ARRAYSIZE];

...

cout << *myArray << endl;      // Output 1st element of array (instead of myArray[0])
cout << *(myArray+4) << endl;  // Output 5th element of array (instead of myArray[4])```

3. so now curiously how would the declaration of an array factor into the use of the [] operator? I mean is it a matter of operator overloading?
I don't think it is operator overloading. This statement:

int myArray[10];

creates an int* called myArray and assigns it the address in memory of the first int in the array, with the other ints layed out consecutively in memory after the first int. So, this statement:

int* pArray = myArray;

just assigns the address stored in myArray to pArray. They are both pointers, so the subscript operator works the same on both of them.

There is an important difference between the two pointers, though: an array name is a constant pointer, which means the address stored in the array name cannot be changed.

4. thanks for the info guys...and hk_...I comepletely forgot about the other notation! That's good information to hold on...is there a good reason to ever use pointer notiation on arrays or will subscripting generally suffice?

5. Choices always allow you more freedom. For instance, you could do this:
Code:
```#include<iostream>
using namespace std;

void f(int* p)
{
cout<<*(p + 1)<<endl;
}

int main()
{
int myArray[3]={10, 20, 30};
int* p = myArray;

f(myArray);
p++; //moves pointer to the second element of the array;
f(p);

return 0;
}```
As an exercise, try writing the same program using only subscripts.

6. > creates an int* called myArray
No it doesn't.

Almost everything you've said about arrays and pointers there is wrong.
http://www.eskimo.com/~scs/C-faq/s6.html

7. Sorry, I'll have to study up on them, then. Thanks. I know when I do this:

cout<<myArray<<endl;

I get an address, so that leads me to believe it's a pointer. And, when I do this:

Code:
```int anotherArray[3] = {1,2,3};
myArray = anotherArray;```
I get an error saying myArray is not an l-value, so that leads me to believe it's a constant pointer.

8. yeah I'm gonna have to agree...in my limited knowledge...I think 7stud's right but I dont know what the hell I'm talking about...

Hmm...ok...I read question 6.2 on that link of yours...it makes sense...the end result is all the same, it's just how the compiler creates it that's different.

9. yeah I'm gonna have to agree...in my limited knowledge...I think 7stud's right
It would be wiser to side with Salem on any issue.

10. lol 7stud...I edited myself...just trying to be supportive hehe

11. I read it too, but I don't get it. It doesn't explain what an array is, it just says it's not a pointer. I find that hard to swallow since when you output the array name it is an address.

Hopefully, after Salem gets done bloodying my knuckles with a ruler, he/she will explain the details.

12. I guess this is the crux of the matter:

Code:
```int myArray[3]={10, 20, 30};

cout<<myArray<<endl;          //006BFDEC
int* p = myArray;

cout<<sizeof(myArray)<<endl;  //12
cout<<sizeof(p)<<endl;        //4```
That says that myArray takes up 12 bytes in memory(4 bytes per element), but p takes up only 4 bytes in memory. The implication is that p is a variable in memory that stores the address of myArray, and that address only takes up 4 bytes. On the other hand, myArray is an object at some location in memory that takes up 12 bytes. Apparently the << operator is defined so that it returns the address of an array(versus it's members or something else), and that makes it appear as if myArray is a pointer when it's actually an object.

Therefore, the difference between p and myArray is one level of indirection: p is a variable pointing to a location in memory, and myArray is an object at that location.

However, I'm not sure what benefit making that distinction gains us. It does not appear that an array can be passed by value like other objects. When you pass the array name to a function, the function can change the array permanently--just as if the array name were a pointer:

Code:
```#include<iostream>
using namespace std;

void f(int array[])
{
array[0] = 14;  //changes first element of the array
}
int main()
{
int myArray[3]={10, 20, 30};
cout<<myArray<<endl;

int* p = myArray;

cout<<sizeof(myArray)<<endl;
cout<<sizeof(p)<<endl;

f(myArray);
cout<<myArray[0]<<endl;  //14

return 0;
}```

13. >>Apparently the << operator is defined so that it returns the address of an array
More likely, there is no << operator defined at all, and the array is implicitly converted to a pointer

>>I'm not sure what benefit making that distinction gains us.
One benefit is simply that it is destroyed automatically when it goes out of scope. This prevents accidental memory leaks, semi-accidental memory leaks (i.e. uncaught exception), and saves you a bunch of time and trouble in freeing memory. I suspect, however, that this garbage-cleaning relies on the array being fixed-size - and therefore pointers are necessary to use dynamic arrays. In any case, it's sometimes handy to create an array and then assign a pointer to its address. An example, used for optimization:
Code:
```const int size = 640 * 480;
unsigned char theImage[size];
for(int i = 0; i < size; ++i)
{
//do something with theImage[i].. does implicit multiplication operation, very slow!
}
//OR
unsigned char* iterator = theImage;
unsigned char* end = iterator + size;
for(; iterator != end; ++iterator)
//do something with (*iterator).. fast dereference operation!```
Just an example of the sort of fun stuff you can do with pointers, that you can't do with arrays

Popular pages Recent additions