passing array of structures to function

This is a discussion on passing array of structures to function within the C Programming forums, part of the General Programming Boards category; dear smart folks, sorry if this is a trivial question. I am trying (in MSVC++ but its a C not ...

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

    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)
    [code]
    typedef struct
    {
    int cc;
    float geno[MAXLOCI], *covar, score;
    } DATAtype;
    [\code]
    and declared as an array
    [code]
    DATAtype orig[MAX_PATTERNS]
    [\code]
    and I call the function PermP
    [code]
    double PermP(INPUTtype inputs, float tval , DATAtype *orig, DATAtype *permed,int pteston, NNET2type Net, float *totsqerr, int *index, PATTERNtype InPatterns,PATTERNtype OutPattern);
    [\code]
    with the call
    [code]
    ppval=PermP(inputs,tval,&orig, ....
    [\code]
    now I thought to pass by ref passing an address and receiving as a pointer to DATAtyoe was correct.
    this link http://www.iota-six.co.uk/c/33_struc_s.htm 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
    Posts
    102
    use tags..
    Code:
    ....
    Saravanan.T.S.
    Beginner.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    Code:
    void function( int [] );
    
    ...
    
    int array[BUFSIZ];
    
    function( array );
    function( &array[0] );
    Oh, and use / not \ for your code blocks.

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

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    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, 03:44 PM
  2. passing an array to function
    By waysgoose in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 04:59 AM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM

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