Thread: C passing 2D arrays to function

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    10

    C passing 2D arrays to function

    Hi,

    passing an array or a 2D array is causing me some trouble.

    can you please check the way i am doin it?

    Here is how i define my arrays and pass them:
    Code:
    ///////////////////////////////////////////////////////////////////////////////////////
    main.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include "NN3.h"
    
    int main()
    {
         real_T u[2][3] = {
                            {0.8147,    0.1270,    0.6324},
                            {0.9058,    0.9134,    0.0975}
                        };
        
         real_T D[1][3] = {0.7922,    0.6557,    0.8491};
                        
         real_T u_test[2][3] = {{0.9572,    0.8003,    0.4218}, 
                                    {0.4854,    0.1419,    0.9157}
                             };
                
         real_T D_test [1][3] ={0.2785,    0.9575,    0.1576};
        
        
         y = NN3(u,*D, u_test, *D_test);
    
        
        return 0;
    }
    //////////////////////////////////////////////////////////////////////////////////////////
    the function prototype is defined as:
    Code:
     void NN3(const real_T eml_u[6], const real_T eml_D[3], const real_T
     eml_u_test[6], const real_T eml_D_test[3], real_T eml_y[3])
    I dont want to change the function prototype. I would rather change the way i am passing the arrays in main.c

    Thanks,

    MAS

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Since your function only accepts single-dimension arrays, you can probably get away with passing your 2D arrays as pointers to the first element.

    Say
    NN3( &u[0][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.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    Since your function only accepts single-dimension arrays, you can probably get away with passing your 2D arrays as pointers to the first element.

    Say
    NN3( &u[0][0],
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Passing 2d arrays to a function
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2005, 09:30 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

Tags for this Thread