Thread: expected 'double **' but argument is of type 'double *'

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    43

    expected 'double **' but argument is of type 'double *'

    I'm lost here and I don't really understand what the problem is. If someone could point it out real quick I'd be very grateful!

    Code:
    void payarray(double *payrate[10]);
    void Summary();
    
    main()
    {
    double pay[10];
    int n;
    
    payarray(&pay[10]); // <------ ERROR here//
    }
    
    
    void payarray(double *payrate[10]){
         
         FILE *fp;
         int i;
         
         fp=fopen("payrates.txt", "r");
         if (fp == NULL){
            printf("Error; No file detected\n");
            exit(1);
         }
         for(i=0;i<15;i++){
         fscanf(fp, "%lf\n", &payrate[i]); //scans file//
         }
         for(i=0;i<10;i++){
         printf("%.2lf \n", payrate[i]); //output payrate//
         }
    }
    I've tried changing it around as much as I could but I still don't have it figured out.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    You're passing the address of the 11th element (which doesn't exist btw) of the array pay, but receiving an array of 10 pointers to doubles.

    This is what you want:

    payarray(&pay);

    This part is also an error. You allocated only 10 elements, but you're attempting to access 15.

    Code:
         for(i=0;i<15;i++){
         fscanf(fp, "%lf\n", &payrate[i]); //scans file//

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    &pay[10] is the address of index 10 (the 11th element) of array pay.

    I'm not actually sure why you are trying to pas the address of the array around.
    Code:
    void payarray(double *payrate[10]);
    That's an array of 10 pointers to doubles. I doubt that's what you want.


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

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Quote Originally Posted by Cynic View Post
    You're passing the address of the 11th element (which doesn't exist btw) of the array pay, but receiving an array of 10 pointers to doubles.

    This is what you want:

    payarray(&pay);

    This part is also an error. You allocated only 10 elements, but you're attempting to access 15.

    Code:
         for(i=0;i<15;i++){
         fscanf(fp, "%lf\n", &payrate[i]); //scans file//
    I've tried the change you mentioned but it's still not working. The code is now:
    Code:
    void payarray(double *payrate[10]);
    void Summary();
    
    main()
    {
    double payrate[10];
    int n;
    
    payarray(&payrate);
    Summary();
    
    
    scanf("%d");
    }
    
    
    void payarray(double *payrate[10]){
         
         FILE *fp;
         int i;
         
         fp=fopen("payrates.txt", "r");
         if (fp == NULL){
            printf("Error; No file detected\n");
            exit(1);
         }
         for(i=0;i<15;i++){
         fscanf(fp, "%lf\n", &payrate[i]); //scans file//
         }
         for(i=0;i<10;i++){
         printf("%.2lf \n", payrate[i]); //output payrate//
         }
    }
    The errors I get are:
    27:1 [Warning] passing argument 1 of 'payarray' from incompatible pointer type
    27:1 expected 'double **' but argument is of type 'double (*)[10]'

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Well, as Quzah said, you really don't need to pass the address of an array.

    Call like this:

    payarray(payrate);

    Receive like this:

    void payarray(double *payrate)

    and fix that loop.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    But how do I pass the arrays when I call the function in main? I'm not understanding this part.

    Here is my code:
    Code:
    void payrate(double *rate);
    void Summary();
    
    main()
    {
    FILE *fp2;
    int empnum[6], paygroup[6], n;
    char lastn[6][12];
    char firstn[6][12];
    double hours[6];
    double gross[6];
    double rate[10];
    int OT;
    
    fp2=fopen("emp.txt", "r");
    if (fp2 == NULL){
         printf("Error; No file detected\n");
         exit(1);
         }
    for(n=0;n<6;n++){
         fscanf(fp2, "%d:%[^:]:%[^:]:%d:%lf\n", &empnum[n], lastn[n], firstn[n], &paygroup[n], &hours[n]);
         }
    for(n=0;n<6;n++){
         printf("\n%04d, %s, %s, %d, %.1lf\n", empnum[n], lastn[n], firstn[n], paygroup[n], hours[n]);
         if(hours[n]>44){
              printf("\nrate is %.2lf\n", rate[n]);           
              gross[n] = hours[n]*rate[n];
              printf("Gross is %.2lf", gross[n]);
              }
         }
    
         fclose(fp2);
         
         // gross pay //
         
    payrate(rate);
    Summary();
    
    
    scanf("%d");
    }
    
    void payrate(double *rate){
         FILE *fp1;
         int i, n;
         double payrate[10];
         
         fp1=fopen("payrates.txt", "r");
         if (fp1 == NULL){
           printf("Error; No file detected\n");
           exit(1);
           }
         for(i=0;i<15;i++){
              fscanf(fp1, "%lf\n", &rate[i]); //scans file//
              }
         for(i=0;i<10;i++){
              printf("%.2lf \n", rate[i]); //output payrate//
              }
         fclose(fp1);
         }
    I need to pass the 'rate' in each array from the 'void payrate' function.
    I thought it would be something like:
    void payrate(double *rate[i])
    or something to that effect.

    Can you please clarify?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    void foo( int array[] )
    {
        array[ 0 ] = 45;
    }
    int main( void )
    {
        int array[2] = { 0, 2 };
        
        printf( "array has: %d and %d\n", array[ 0 ], array[ 1 ] );
        printf( "calling foo( array )\n" );
        foo( array );
        printf( "array has: %d and %d\n", array[ 0 ], array[ 1 ] );
    
        return 0;
    }
    If you are confused on some point, try to make a simple program to clarify what it is you are confused about. The smaller the better.


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

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Okay Mr.Quzah I think I figured that out but I've got another question for you:

    Right now I have a "paygroup" and "payrate".

    Now, for example:
    Payrate 1 is 7.75
    Payrate 2 is 8.00

    Here's the issue; Paygroup is not ordered small to big because they are stored according to data in the file:

    0001:Satriani:Joe:6:38.0
    0002:Vai:Steve:1:44.5
    0003:Morse:Steve:10:50.0

    You see that '6' after 'Joe:' ? That's the paygroup. Notice that the first entry is 6, the second is 1, the third is 10.

    So my question is how would I match 6 to rate 6 (15.75)? Because right now it is stored as the first spot in the array, so not actually 6. I don't know how to match them.

    edit:
    Essentially what I'm asking is how do I compare the actual numerical value of what's stored in the array rather than the arrayposition itself.

    Please help me and thank you!
    Last edited by MC++; 04-15-2012 at 05:35 PM.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    An unscripted array is the symbolic name of the address where the array starts. By passing:

    Code:
    payrate(rate);
    you are passing the address of rate.

    By receiving:

    Code:
    void payrate(double *rate){
    You are receiving that address into rate (local to the function payrate) and can access elements by subscripting or pointer arithmetic.

    I need to pass the 'rate' in each array from the 'void payrate' function.
    "rate" is the array. It's not multiple arrays, just the one. The individual doubles in that array are called elements. Now, you're passing an array and looping through it. You don't want to pass an individual element and try to loop through it, that would be a compile error (attempting to subscript a single). So far, it looks good, but you are still running off the end of the array with:

    Code:
         for(i=0;i<15;i++){
              fscanf(fp1, "%lf\n", &rate[i]); //scans file//
              }
    Remember, you're only defining rate as a 10 element array, but you're going 5 elements past the end.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    I already changed the array size to 10. Took care of that one a while ago, sorry I didn't mention that.

    Can you help me with my latest issue? Around 2 posts prior to yours.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You get and put things into array elements the same way, by accessing that particular spot. Just like I showed you in my example. The only difference is that instead of using = to assign something, you use == to compare what's there.


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

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    43
    Quote Originally Posted by quzah View Post
    You get and put things into array elements the same way, by accessing that particular spot. Just like I showed you in my example. The only difference is that instead of using = to assign something, you use == to compare what's there.


    Quzah.
    I'm sorry to bother you so much, but can you give me an example if mine is wrong?

    is it simply:
    array[0] == array[1]

    Thank you so much for all your help today Quzah. You are like a wizard, and computing language is like modern day esoterica.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new to C, expected expression before 'double' message
    By jadejones in forum C Programming
    Replies: 3
    Last Post: 01-02-2012, 10:43 AM
  2. Issue with "double" argument type
    By seanksg in forum C Programming
    Replies: 10
    Last Post: 03-25-2011, 03:22 AM
  3. Double argument question
    By towed in forum C Programming
    Replies: 4
    Last Post: 06-08-2010, 01:39 PM
  4. Replies: 2
    Last Post: 12-07-2009, 10:26 PM
  5. Passing a double array to a function as an argument
    By Glirk Dient in forum C++ Programming
    Replies: 20
    Last Post: 09-10-2003, 02:54 PM