Three dimensional arraY addresses

This is a discussion on Three dimensional arraY addresses within the C Programming forums, part of the General Programming Boards category; I am working on three dimensional problem and facing certain issue .I am trying to make you understand the issu ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    13

    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

    please clear my doubts

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142
    Quote Originally Posted by er.rohan88 View Post
    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.
    Last edited by pheininger; 08-17-2010 at 07:11 AM. Reason: Added some additional comment and softened "exist".

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote 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

    Quote Originally Posted by pheininger
    I would have to read the C standard closely to understand what the "official" handling would be.
    Undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.
    Never re-write code unless the user benefits

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #7
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.
    Never re-write code unless the user benefits

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by johnggold View Post
    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.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  9. #9
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    142
    Quote Originally Posted by johnggold View Post
    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.
    Quote Originally Posted by Adak View Post
    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.
    Last edited by pheininger; 08-17-2010 at 10:24 AM. Reason: Added sentence about commenting.

  10. #10
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    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.
    Never re-write code unless the user benefits

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #12
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    Thank you Adak for making my day - I know just how you feel!!!!
    Never re-write code unless the user benefits

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Location
    England
    Posts
    86
    Quote Originally Posted by Salem View Post
    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.
    Never re-write code unless the user benefits

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 12:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21