Pointer notation for Multi-dimensional arrays...

This is a discussion on Pointer notation for Multi-dimensional arrays... within the C Programming forums, part of the General Programming Boards category; Question: What is the pointer notation for multi-dimensional arrays. For example, for a single array, I know you can write ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Pointer notation for Multi-dimensional arrays...

    Question:

    What is the pointer notation for multi-dimensional arrays. For example, for a single array, I know you can write it as *(p+i), where i can increase or decrease. How would I do this for a multi-dimensional array?

    Thanks

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    for a specific example in my code:

    Code:
    for(i=0; i< M-1; i++)
                {
                  if(age[i][0] == 0)
                    {
                      addr_temp = i;
                      j=0;
                      do
                        {
                          name[addr_temp][j] = name2[j];
                          j++;
                        }
                          while(name2[j] != '\0');
    
                      break;
                    }
                }
    Can someone show me how I would transfer a single array (name2[j]) to a multi-dimensional array (name[addr_temp][j]) ? How would I assign and declare the pointers for the multi-dimensional array pointer?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Remember that the farthest-to-the-right index changes fast, so adding one position changes the rightmost index. So name[a][b] lives at the a*(size of second dimension)+b location.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    another way would be to set the pointer to the first element [row=0][col=0] of the 2D array as in p = name[0] and then use the pointer and an offset notation *(p + i)

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    well, I need the change the array accordingly. So if I had an array with a dimension like this:

    array[10][15] (so it has 10 rows, and 15 columns)...

    the way I would be able to access a specific row and all the columns would be this (say row 6): ?

    a = array

    a*(6) + i (where i will increment from 0-14)?

    I'm not quite sure if I have the notation down...

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    if you wanted to access the last element of row 6 the notation is...
    *(p + i) where the offset i = num_of_cols X row + num_of_cols

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    i = num_of_cols X row + num_of_cols

    so if I had an array of array[10][15], and wanted to access last element of row 6, then it would be

    *(p+i) where i = (15)(6) + 15 ?

    I'm just trying to use a real example so I can make sure I understand it fully, thanks

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why don't you write a small example where you fill each row and colum with a different value (e.g. a multiplcation table), and then write another function to extract the data and see that you get it rigth. You could also write a 3 dimensional multiplication array (x * y * z), and see that you get that right.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    but is the way that I am presenting it, correct?

  10. #10
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Multidimensional arrays aren't stored as rows and columns...

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Try it out as matsp adviced and if you run into problems you can always ask back here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Help with Pointer Arrays
    By siLent0 in forum C Programming
    Replies: 10
    Last Post: 04-15-2008, 03:57 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 11:53 AM

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