Thread: passing array of structures to function

  1. #1
    Registered User
    Join Date
    Mar 2002

    passing array of structures to function

    dear smart folks, sorry if this is a trivial question.
    I am trying (in MSVC++ but its a C not C++ program)
    to pass a 1D array of structures to a function by "reference" (not value, I want the function to be able to change values).

    the structure is typedeffed (outside main) using defined constants (but ultimately I will want to malloc its size which may cause problems again)
    typedef struct
    int cc;
    float geno[MAXLOCI], *covar, score;
    } DATAtype;
    and declared as an array
    DATAtype orig[MAX_PATTERNS]
    and I call the function PermP
    double PermP(INPUTtype inputs, float tval , DATAtype *orig, DATAtype *permed,int pteston, NNET2type Net, float *totsqerr, int *index, PATTERNtype InPatterns,PATTERNtype OutPattern);
    with the call
    ppval=PermP(inputs,tval,&orig, ....
    now I thought to pass by ref passing an address and receiving as a pointer to DATAtyoe was correct.
    this link seems to say so.
    but there seems to be some confusion over passing a structure or an array of structures that I can't resolve as I get a warning error in MSVC++

    c:\snpnet\netcall.c(746) : warning C4024: 'PermP' : different types for formal and actual parameter 3
    c:\snpnet\netcall.c(746) : warning C4047: 'function' : 'struct DATAtype *' differs in levels of indirection from 'struct DATAtype (*)[1000]'

    the 1000 refers to MAX_PATTERNS.
    by the way the program seems to work fine !!! but I'm keen to sort this error before I change it to use malloc.

    sorry its a long tedious query

    many many thanks in advance for any advice (and thanks for your help on a couple of previous occasions too)

  2. #2
    Registered User
    Join Date
    Jul 2003
    use tags..

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    When passing an array to a function, just use the name of the array, not the address of the array. If you want to use the address, use the address of the first element:
    void function( int [] );
    int array[BUFSIZ];
    function( array );
    function( &array[0] );
    Oh, and use / not \ for your code blocks.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2002
    many thanks to you both, that seems to work fine !!
    simple when you know how !
    sorry about the / not \ business !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-31-2009, 02:44 PM
  2. passing an array to function
    By waysgoose in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 03:59 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM