Thread: please help : arrays/parameters

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    please help : arrays/parameters

    Hey, A couple of questions.
    I am unsure how twodimensional arrays work
    This is what I have.
    //calling the function
    calc_pay(emp_counter, *wg_hr_py);
    //function calc_pay
    void calc_pay(int emp_counter, double wg_hr_py[][MAX_COL]);
    {
    int i, j=0;
    for(i = 0; i <emp_counter; i++)
    {
    wg_hr_py[i][j+3] = wg_hr_py[i][j] * wg_hr_py[i][j+2];
    }
    }
    This isn't working and I was wondering Why?
    I am mostly having trouble with the calling of the two dimensional array and how to put it in the parameters. Pllleeaase help I have been working for days on two dimensional arrays and I'm getting no where.
    Thanks


  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    This would work if 'wg_hr_py' in the calling code was a pointer to a pointer to a double but I doubt it is. Remove the '*' before it in the call to 'calc_pay' and see if it works then.
    // Gliptic

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This would work if 'wg_hr_py' in the calling code was a pointer to a pointer to a double
    No.

    wg_hr_py in the caller should be declared as
    &nbsp; double wg_hr_py[MAX_ROW][MAX_COL];
    and the call is simply
    &nbsp; calc_pay(emp_counter, wg_hr_py);
    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.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    It would work!

    First, the '*' dereferences the pointer to the pointer and just the pointer is passed to the function. However, I don't think it is a pointer to a pointer so the '*' should be removed.
    // Gliptic

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Jax: in August I posted a question about arrays also, so I just thought you might want to have a look at them. The answers are from "Prelude" and "ali c".

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > It would work!
    Ok, given
    double wg_hr_py[MAX_ROW][MAX_COL];

    Show me how you would prototype and write a function using whatever combination and number of *'s you want, and how you would then call that function.
    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.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I meant if wg_hr_py was a pointer to a pointer to a double:

    double **wg_hr_py;

    Suppose that some space is allocated at the pointer position.

    If the calc_pay function was called like this:

    calc_pay(emp_counter, *wg_hr_py);

    It would work. The pointer to the double is passed.
    // Gliptic

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Suppose that some space is allocated at the pointer position.
    How?
    I can allocate memory to this pointer in 2 ways.

    One way is consistent with the way the pointer is declared, but incompatible with the way the function is declared

    The other way is inconsistent with the way the pointer is declared, but compatible with the way you call the function.

    double ** is not the same as double[][], and they don't magically become the same thing when you call a function.
    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.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Both are pointers!
    // Gliptic

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Both are pointers!
    Yes they are, but what types are they?

    double **ptr;
    When passed to a function has a type of double**

    double arr[x][y];
    When passed to a function has a type double (*)[y]
    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