Use pointer for multidimension array

This is a discussion on Use pointer for multidimension array within the C++ Programming forums, part of the General Programming Boards category; I have learnt how to use pointer for multidimension array. this is my example: Code: int trace [3][4]; int (*ptr)[4] ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Use pointer for multidimension array

    I have learnt how to use pointer for multidimension array. this is my example:
    Code:
    int trace [3][4];
    int (*ptr)[4] =trace;
    After that,I don't know how to use pointer ptr to process elements of trace array.
    who can teach me, please.

    thanks for tons

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >int(*ptr)[4] =trace;
    This... AFAIK... is useless.
    You can already use trace as a pointer.
    "trace[i][j]" gives the required element, so does " *(trace+i*4+j) "
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Yes, I know this way too. there three ways same same like that to touch trace[i][j]:
    Code:
    1) *(*(trace +i)+j)
    2)*(trace[i]+j)
    3)*(trace+i)[j]
    these ways easy to understand why. but the above example, in my book say that, but they don't say how to use this way
    I think will exist a method to use this

    @: manasij: I see that you like to use slang to talk today, I have learnt word: asap: as soon as possible. and now, new word:afaik

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    afaik = as far as I know. You can find most of these by googling "acronym afaik".

    I agree with manasij7479 that using a pointer here is generally more awkward than using the index, but there are times when a pointer is easier or tidier. So if you need an example of how to walk a matrix that way:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(void) {
    	int ray[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9} },
    		(*p1)[3] = ray, *p2 = p1[0];
    
    	while (p1 - ray < 3) {
    		if (p2 - *p1 == 3) {
    			cout << endl;
    			p1++;
    			p2 = p1[0];
    		} else {
    			cout << *p2 << ' ';
    			p2++;
    		}
    	}
    
    	return 0;
    }
    It is very important when doing this that the pointers are correctly typed!

    Some things to notice: p1 is effectively the same type as "ray" (technically, ray is an array while p1 is just a pointer, but ray implicitly converts to a pointer in context). However, p2 is not the same type as p1, and so when doing arithmetic with addresses, you must dereference (via *) p1 to make them equivalent, as on line 10 (just as you must dereference p2 on line 15 to get a value).
    Last edited by MK27; 11-11-2011 at 08:08 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question

    Oh, thanks very much to help me solve my problem. I have understand your code. It likes this idea:
    Code:
    int trace[4];
    int ptr*=trace; 
    // So: (ptr+1)=trace[1]; (ptr+2)=trace[2];
    And, I have an other question bulding from above problem:
    I have this code:
    Code:
       int trace[3][4];
       int (*ptr)[3]=trace;
    the complier will notice this error:cannot convert 'int (*)[4]' to 'int (*)[3]' in initialization.
    I know C++ associate demension of array with type of variable, and int(*)[4] and int(*)[3] cannot convert together. But, why this line:
    Code:
     int(*ptr)[3]=trace
    , C++will know type of trace is int(*)[4]. Can you teach me more, please.

    thanks
    Last edited by hqt; 11-11-2011 at 08:36 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,415
    An array is converted to a pointer to its first element. So, when you have an array of 3 arrays of 4 ints, it is converted to a pointer to an array of 4 ints, i.e., int (*)[4]. This is different from, and incompatible with, a pointer to an array of 3 ints.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 01:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  3. Replies: 5
    Last Post: 09-21-2009, 11:48 AM
  4. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 11:03 PM
  5. Replies: 1
    Last Post: 03-24-2008, 10:16 AM

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