Problem passing structure with array into function

Discussion in 'OT Technology' started by userlain, Jul 13, 2005.

  1. userlain

    userlain I use proper grammar on the internet.

    Joined:
    Jun 17, 2004
    Messages:
    1,245
    Likes Received:
    0
    Location:
    USA
    The structure:
    Code:

    typedef struct{
    int val[256];
    }values;​



    The function(needs to pass information into the structure array 'val'):
    Code:

    void changeval(values *obj, int v, int a){
    (*obj).val[v]=a;
    }​


    ^^That piece is the piece I am having issues with...


    And finally, the call:
    Code:

    int main(){
    values item;
    changeval(&item,0,50);
    return 0;
    }​





    Ok, why can't I pass a user-defined datatype into a function and be able to access the array that is in the object? [​IMG]
     
  2. coKestar

    coKestar New Member

    Joined:
    Feb 23, 2005
    Messages:
    46
    Likes Received:
    0
    BOOL changeval(values* v, UINT bound, int val)
    {
    if (bound >= 256)
    return FALSE;

    if (v)
    v->val[bound] = val;
    else
    return FALSE;
    }
     
  3. EkriirkE

    EkriirkE Zika Xenu OT Supporter

    Joined:
    Jan 11, 2004
    Messages:
    14,799
    Likes Received:
    0
    Location:
    Dublin & San Francisco, CA
    coke has the propper way of accessing array/struct pointers (and added some fault protection if your caller is stupid)

    use -> instead of casting the pointer


    v->val[] not (*v).val[]
     

Share This Page