Thread: Array of ints

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Array of ints

    Does anyone know how to pass an array of ints to a function?
    i tried
    Code:
    int a[2][2] = { {1, 2}, {3, 4} };
    function(a);
    
    void function(int **a) {
      printf("%d\n", a[1][1]);
    }
    and it didn't seem to work.
    Any suggestions?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void function(int **a) {
    Should be one of these - choose which is most readable

    void function(int a[2][2] ) {

    void function(int a[][2] ) {

    void function(int (*a)[2] ) {
    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.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    You could also do:

    void function(int a[][])

    Right, Salem?
    1978 Silver Anniversary Corvette

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void function(int a[][])
    > Right, Salem?
    No, not right.

    You can only leave the leftmost [] empty.
    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.

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I see. You can have:
    Code:
    void function (int arr[]); /* legal */
    void function (int arr[][2]); /* illegal */
    But, doesn't that limit what you can do with something like this?

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    Seems to work now.
    Thanks

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    void function (int arr[][2]);

    This is legal. Nothing wrong with it.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    Unregistered
    Guest
    hi,
    could anyone pls a complete sourcecode showing how to use the function in main? i tried several versions that didn't work. and how can i pass a pointer to the array, not just a copy of it?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > and how can i pass a pointer to the array, not just a copy of it?
    Arrays are always passed as a pointer, you have no choice.

    > could anyone pls a complete sourcecode showing how to use the function in main?
    See the original poster
    &nbsp; function(a);
    is the way
    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. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  2. array of ints
    By barim in forum C Programming
    Replies: 4
    Last Post: 01-01-2005, 05:21 AM
  3. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Replies: 4
    Last Post: 11-18-2001, 07:29 PM