Thread: Passing a pointer to a 2D array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    Passing a pointer to a 2D array

    What is the correct way to pass a pointer to 2D array?

    What I do:

    float X[length][length]

    foo(&X);

    Error:

    warning: passing argument 1 of ‘foo_’ from incompatible pointer type

    The program will still compile and run properly, but I hate these compilation errors.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Check out the tutorials.

    Code:
    int x[M][N];
    
    void func(int y[M][]);
    
    func(x);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    That's for passing an array.

    I need a pointer to an array,

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dwks
    Check out the tutorials.

    Code:
    int x[M][N];
    
    void func(int y[M][]);
    
    func(x);
    Perhaps you meant:
    Code:
    void func(int y[][N])
    Quote Originally Posted by OakRand
    That's for passing an array.

    I need a pointer to an array,
    How do you think arrays are passed?
    Code:
    void func(int (*y)[N])
    [edit]http://c-faq.com/aryptr/pass2dary.html
    Last edited by Dave_Sinkula; 05-17-2006 at 05:37 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    I don't need to know how to define the function, but how to specify the 2d array as a pointer in the function call.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Code:
    void func(int (*y)[N]);
    ...............
    int array[M][N];
    func(array);

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    From, what I can gather from the lack of usefull responces to my thread on the type of a 2D array, there is no way to do this.
    Last edited by King Mir; 05-17-2006 at 10:18 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop spamming the forums with your whining. You've already been answered.

    [To the OP]
    As to this topic, there are different ways to interpret this. Do you want a "pointer to a two dimensional array", or do you just want a function to accept a 2D array? They are two different things. Consider:
    Code:
    void foo( int x );
    This function cannot update the variable 'x' passed to it so it affects the variable outside the function. That is why we must use a pointer to it:
    Code:
    void foo( int *x );
    This is not true for arrays. Arrays degrade to a pointer to the first element, thus, changes to them affect the array itself. Thus:
    Code:
    void foo( int array[] );
    Any changes to the array passed to this function will affect the origional array. Therefore, we don't need a pointer to an array. We can have one however...
    Code:
    void foo( int (*p2a)[] );
    This is (takes, rather) a pointer to an array of integers.

    You can also have a pointer to a two dimensional array. Is that really what you want? Or is all you want is a way to pass an array to a function?
    [/To the OP]

    Now, back to you. Shut the hell up and stay in your own thread. Your question is in no way related to this, so stop trying to garner sympathy.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What is the correct way to pass a pointer to 2D array?
    > float X[length][length]
    > foo(&X);
    Is there some benefit to passing a pointer to the array rather than the array itself?
    Remember, arrays are always passed as a pointer, so foo(X) and foo(&X) achieve pretty much the same effect.

    Yes, you can use an & if you really want to, providing you declare the function properly in the first place.
    Code:
    #include<stdio.h>
    #define LEN 10
    
    void f1 ( int a[LEN][LEN] ) {
      a[0][0] = 0;
    }
    void f2 ( int a[][LEN] ) {
      a[0][0] = 0;
    }
    void f3 ( int (*a)[LEN] ) {
      a[0][0] = 0;
    }
    void f4 ( int (*a)[LEN][LEN] ) {
      // watch the extra level of indirection needed
      (*a)[0][0] = 0;
    }
    
    int main ( void ) {
      int arr[LEN][LEN];
      f1( arr );
      f2( arr );
      f3( arr );
      f4( &arr );
      return 0;
    }
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Pointer to 2D array?
    By meredithm in forum C Programming
    Replies: 1
    Last Post: 10-30-2008, 01:11 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. sending or recieving a[][]to a function
    By arian in forum C++ Programming
    Replies: 12
    Last Post: 05-01-2004, 10:58 AM