1. ## Three dimensional arraY addresses

I am working on three dimensional problem and facing certain issue .I am trying to make you understand the issu by certain example.
Code:
```int arr[3][2][2]={1,2
,3,4,

5,6
,7,8,

9,10,
11,12};

printf("arr is %d , %d",*(*(arr+2)+1),*(*(arr[0]+2)+1));```
I want to print arr[0][2][1]:

it shoul print 6 ,6 where as it prints 129665 , 6.

I am not able to understand as I think arr[0],arr shoul give the base address

2. Perhaps you should post the smallest and simplest compilable program that demonstrates the problem. At the moment it is not clear to me what exactly you were trying to print (other than arr[0][2][1], for which you do get the correct result).

3. Originally Posted by er.rohan88
Code:
```int arr[3][2][2]={1,2
,3,4,

5,6
,7,8,

9,10,
11,12};

printf("arr is %d , %d",*(*(arr+2)+1),*(*(arr[0]+2)+1));```
I want to print arr[0][2][1]:
...
arr[0][2] does not "exist". Your second dimension is 2 so arr[0][0] exists and arr[0][1] exists, but arr[0][2] is one past the end of arr[0]. Obviously, there is a reachable value in a three dimensional array at that position. I would have to read the C standard closely to understand what the "official" handling would be. If it were my code, I would just try to stick to the straight forward approach of not using 2 to index the second dimension.

4. Originally Posted by pheininger
arr[0][2] does not "exist".
Oh dear, it seems that I missed both this fact and the printf line at the bottom

Originally Posted by pheininger
I would have to read the C standard closely to understand what the "official" handling would be.
Undefined behaviour.

5. Code:
`int array[MAXVAL+1];`
Always add one more to the array than you think you need. That way you can always populate arrays from 1 to MAXVAL which is more intuitive than starting from 0.

6. I have to disagree with you on this John.

C code I've run across supports the array[MaxElements] in the range of 0 to MaxElements-1, very heavily.

After a period of adjustment, it seemed normal to use that standard type of idiom. I believe it's like learning any foreign language - don't just translate from the new language, into your native language. Instead, start thinking ONLY in the new language, when you speak or hear it. Dive right on in.

Constantly translating back and forth, with only make it more difficult to master the new language. Granted, there aren't many idiomatic expressions in C, but analogies always fall apart after a bit, don't they?

7. It's about being intuitive. Starting from zero is contrary to everyday life. You don't count starting from zero do you?

Also arrays starting from zero is not universal - there are plenty of languages that don't.

C,C++ like any languages are application servants not the masters, although sometimes it is all too easy to forget this.

Also the language analogy is unfounded. People quite fluent in languages will often get the grammar wrong whilst remaining quite understandable.

8. Originally Posted by johnggold
Code:
`int array[MAXVAL+1];`
Always add one more to the array than you think you need. That way you can always populate arrays from 1 to MAXVAL which is more intuitive than starting from 0.
For the OP: You should probably never do this. Not only does this always waste space, but it makes the code far less intuitive than one might think, especially for others who are looking at your code. The convention in C is that array indexing begins at 0.

9. Originally Posted by johnggold
Code:
`int array[MAXVAL+1];`
Always add one more to the array than you think you need. That way you can always populate arrays from 1 to MAXVAL which is more intuitive than starting from 0.
I have to disagree with you on this John.

C code I've run across supports the array[MaxElements] in the range of 0 to MaxElements-1, very heavily.

After a period of adjustment, it seemed normal to use that standard type of idiom. ...
Let me suggest a compromise (or softer) statement based on johnggold's that I think is helpful (or may be not ).

Consider adding one more to the array than you think you need. That way you can ... populate the array from 1 to MAXVAL when it is more intuitive than starting from 0.

I would add to this: If you are indexing arrays from 1, avoid indexing arrays from 0 and from 1 in the same function or file. Remember if your indexing arrays from 1 in C, there still is an element 0 (array[0]). If your indexing arrays from 1 in C, include a comment near the definition of the array and near the code manipulating the array stating you are indexing from 1 on purpose.

10. There are consequences for this approach.

For example, you have to keep adding 1 all over the place - extra computation.

Just take a simple printed list :

Code:
```int array[5] = {21,32,13,24,35};
int i;

/*
To print a list  of values stored in an array - could be anything - a list of items in an invoice, anything read by a user in list form
*/

for( i = 0; i < 5; i++ )
{
// Additional computation on every loop i.e i+1
printf( "Item %d is value %d\n", i+1, array[i] );
}

// Or ....
int array[5+1] = {0,21,32,13,24,35};
int i;

for( i = 1; i <= 5; i++ )
{
printf( "Item %d is value %d\n", i, array[i] );
}```
Space has not been a problem for years, and adding a single record is unlikely to be the straw that breaks the space camel's back.

For inexperienced programmers being intuitive is important - if you're experienced you should not have a problem reading either way.

11. I'd like to agree with you, John,

but I'd feel like such a traitor!

And you're right, of course - any programmer worth a hoot can handle it either way, as long as it's noted (or obvious).

We have chess boards that work this way (having border rows and columns). In fact MY chess board works this way since it's 10 x 10.

12. Thank you Adak for making my day - I know just how you feel!!!!

13. Originally Posted by johnggold
For inexperienced programmers being intuitive is important - if you're experienced you should not have a problem reading either way.
Agreed. That said, I have seen an interesting idea with respect to pedagogy in favour of starting at 0: the loop invariant. Of course, either way the loop will have an invariant, but in your first example in post #10, we could propose this as the loop invariant: i values have been printed so far. It is intuitive to recognise the pattern that i elements of the array have been processed so far; a novice can see that this indeed is true each time control enters the loop body, and that it is also true when the loop terminates.

14. Start at zero and be compatible with just about every other C programmer in the world.

If you start at 1 for no good reason, people will just think you're being weird.

Further, if people other than yourself work on the code (and they all understand that arrays start at zero), then expect plenty of off-by-1 bugs to suddenly appear.

> // Additional computation on every loop i.e i+1
Try to avoid second guessing what any particular compiler might do with that.
It's going to increment i at some point anyway, it could easily give you this for free.

15. Originally Posted by Salem
Start at zero and be compatible with just about every other C programmer in the world.

If you start at 1 for no good reason, people will just think you're being weird.

Further, if people other than yourself work on the code (and they all understand that arrays start at zero), then expect plenty of off-by-1 bugs to suddenly appear.

> // Additional computation on every loop i.e i+1
Try to avoid second guessing what any particular compiler might do with that.
It's going to increment i at some point anyway, it could easily give you this for free.
There you have the problem. Do you want to stay compatible with the world or just a few geeks who think that C/C++ is more important. Probably the same people who insist on using ++i instead of i++ against the rest of the world who don't where it serves no purpose.