Would this C function work?

This is a discussion on Would this C function work? within the C Programming forums, part of the General Programming Boards category; I keep getting these errors of " passing argument 'somenumber' of ‘somefunction’ from incompatible pointer type" So would this function ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    3

    Question Would this C function work?

    I keep getting these errors of "passing argument 'somenumber' of ‘somefunction’ from incompatible pointer type"
    So would this function work?
    *Please assume that the call would be in a main function and this were part of an actual working program.

    Code:
    int checkPassReqs (double *avg1[10], double *avg2[10], char *Pass[10], int *Finals[10], int *ExtraCredit[10], double *Grade[10], int *Assignments[70], int *Midterms[20]); //declaration
    Code:
    
    checkPassReqs (&avg1[10], &avg2[10], &Pass[10], &Finals[10], &ExtraCredit[10], &Grade[10], &Assignments[70], &Midterms[20]); //call
    
    int checkPassReqs (double *avg1[10], double *avg2[10], char *Pass[10], int *Finals[10], int *ExtraCredit[10], double *Grade[10], int *Assignments[70], int *Midterms[20]) //definition
    {
          int i;
    
         for (i=0; i<10; i++)
         {
         *avg1[i] = (*Assignments[(7 * i)] + *Assignments[(7 * i) + 1] + *Assignments[(7 * i) + 2] + *Assignments[(7 * i) + 3] + *Assignments[(7 * i) + 4] +            *Assignments[(7 * i) + 5] + *Assignments[(7 * i) + 6])/7;
         *avg2[i] = (*Midterms[(2 * i)] + *Midterms[(2 * i) + 1])/2;
         }
    
         for (i=0; i<10; i++)
         {
         if (*Pass[i] != 'P')
         *Grade[i] = 0.0;
         if (*avg1[i] < 70.0)
         *Grade[i] = 0.0;
         if (*avg2[i] < 60.0)
         *Grade[i] = 0.0;
         if (*Finals[i] < 60.0)
         *Grade[i] = 0.0;
         if (*Grade[i] != 0.0)
         *Grade[i] = ((*avg1[i] + *avg2[i] + *Finals[i] + *ExtraCredit[i])/3);
         }
    
    
    return 0;
    }


  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I don't know what you're passing it, but your function looks hugely over-complicated. It's senseless to ask us if your function will work, especially when you don't provide the full code. You should be able to test and compile it yourself.

    Also, a few points for improvement/correctness that you should take into account:
    - instead of using a lot of arrays of the same size, make an array of that many structs that contain the elements you need
    - you should calculate averages with loops, instead of hard-coding it
    - you don't need array brackets for calling functions, i'm sure that's at least part of your problem
    - you should use proper indentation and remove formatting so it's easier to read on the forum

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Assuming that these are all one-dimensional arrays, change the declaration to this:
    Code:
    int checkPassReqs (double *avg1, double *avg2, char *Pass, int *Finals, int *ExtraCredit, double *Grade, int *Assignments, int *Midterms); //declaration
    and the call to this:
    Code:
    checkPassReqs(avg1, avg2, Pass, Finals, ExtraCredit, Grade, Assignments, Midterms); //call
    You'll also have to change the code to match the declaration (get rid of the dereferencing asterisks).
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    I just want to know if you see any errors with the passing stuff (pointers, addresses, arguments, etc.) because I don't want to burden you with the whole code. There's like 5x of these long redundant functions.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    3
    Hey man, thanks a lot. I'm just wondering though, if I get rid of all the dereferencing asterisks, will the arguments still be passed by address? My teacher said that if you want the arguments that you passed from main to the function to retain the values that are modified in the function, it is necessary to pass the arguments by address.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > if I get rid of all the dereferencing asterisks, will the arguments still be passed by address
    Irrelevant, the arguments are already passed by address no matter if you dereference later or not.

    > to retain the values that are modified in the function, it is necessary to pass the arguments by address.
    This is true, unless it's already a pointer. Because arrays are passed as pointers, this isn't necessary. Here's a quick example to change the second element of an array to zero:
    Code:
    int function (int *array) 
    {
        *(array + 1) = 0;
    }
    /* ... */
    int array[10] = {0, 1, 2, 3, ..., 9};
    function(array);
    will work, as will:
    Code:
    int function (int array[]) 
    {
        array[1] = 0;
    }
    /* ... */
    int array[10] = {0, 1, 2, 3, ..., 9};
    function(array);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this function work?
    By Harry Reve in forum C Programming
    Replies: 2
    Last Post: 11-06-2011, 08:38 PM
  2. How does this function work?
    By TheWhiffet in forum C Programming
    Replies: 3
    Last Post: 05-10-2011, 07:55 AM
  3. How to work on this function
    By void12 in forum C Programming
    Replies: 1
    Last Post: 02-07-2011, 11:16 AM
  4. cant get my function to work!
    By scaven in forum C Programming
    Replies: 3
    Last Post: 05-08-2003, 03:08 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM

Tags for this Thread


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