# Quicky Question

• 02-05-2005
Krak
Quicky Question
When you create an array, such as int m[10], m is actually a pointer, yes yes?

And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?

The whole m[1] thing is just a cleaner and more understandable way of doing it, yes?
• 02-05-2005
azteched
Quote:

Originally Posted by Krak
When you create an array, such as int m[10], m is actually a pointer, yes yes?

m is an array of 11 ints. However, the name m can be used as a pointer to the first element of the array.

Quote:

And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?
You aren't allowed to add pointers. I assume you meant adding values to pointers. Yes, *(m+1) accesses location [m + sizeof(int)].

Quote:

The whole m[1] thing is just a cleaner and more understandable way of doing it, yes?
Depends what you're doing and your personal preference. Many people find a pointers/iterators approach easy to use - look at the STL for example.
• 02-05-2005
andyhunter
>When you create an array, such as int m[10], m is actually a pointer, yes yes?<

Well, no actually, however m acts as a const reference to the first value in the array.

>And because pointer arithemetic allows you to add pointers of the same data type, you can add to *m with *(m+1) to skip over one "slot", which is equal to the sizeof(int), right?<

yes, you can use pointer arithmatic to walk through an array. However you cannot change the value of m.
• 02-05-2005
andyhunter
Quote:

You aren't allowed to add pointers
umm. Well you can add pointers, you cannot change the value of m because it acts as a const pointer. Adding pointers however is very possible:

Code:

```#include <iostream> using std::cout; using std::endl; using std::cin; int main() {         int myArray[4] = {3, 5, 7, 9};         int* myPointer;                 myPointer = myArray;         for(int i = 0; i < 4; i++, myPointer++) {                 cout << *myPointer << endl;         }         cin.get();         return 0; }```
-Andy
• 02-05-2005
azteched
That's not adding pointers. That's adding integral values to pointers - a completely different thing.
• 02-05-2005
andyhunter
My bad - misunderstood what you meant. :)
• 02-06-2005
CornedBee
> m is an array of 11 ints.

No, it's 10 ints.

> however m acts as a const reference to the first value in the array
Actually, it acts as an immutable pointer to the (mutable, unless said to be const) first element of the array.
• 02-06-2005
azteched
Oops - been using VB arrays :o
• 02-06-2005
Salem