Thread: C programming - incompatible argument type

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    36

    C programming - incompatible argument type

    This is part of a code I am working on. If you need me to post the whole code I will. When I try to compile it tells me "incompatible type for argument 2 of 'par_complex'.

    Code:
    void read_complex(float *z)
    {
        printf("Enter a complex number:");
        scanf("%f%f",&z[0],&z[1]);
    }
    void par_complex(float *z,float *mag,float *ph)
    {
        *mag=sqrt(pow(z[0],2)+pow(z[1],2));
        *ph=atan(z[1]/z[0])*180/3.14159265;
    }
    int main(void)
    {
        float z[2];
        float z1[2];
        float z2[2];
        float z3[2];
        float *p_phase,*p_magnitude,phase,magnitude;
        p_phase=&phase;
        p_magnitude=&magnitude;
        
            read_complex(z1);
        par_complex(z1,p_magnitude,p_phase);
        printf("The magnitude is %f and the phase angle is           
            %f.",magnitude,phase);
    The other floats are for the rest of the problem by the way.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Please post the whole code, because I am not seeing how you could possibly be getting that error from the code that's there.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    I am not sure what happened, but it is working now. Kind of frustrating, but at least it works. Thanks for looking at it anyway!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ahhh... the next problem, I see...

    Ok, last time was all about process... this time it's looking like efficiency...

    To start you need to work on your text formatting just a little... I generally leave vertical whitespace (blank lines) between functions so they are more clearly blocked out in the code. Not a big deal but it does make it easier to read.

    On the point of efficiency... lines 17, 18 and 19, are using pointers to point to existing variables in the same scope. This is not necessary as you can simply do this in line 22...
    Code:
    par complex(z1, &magnitude, &phase);
    ... eliminating two unnecessary variables from your program... and thus two potential causes of error.

    I don't see the error you've listed from this code snippet... is par_complex() prototyped someplace else? If so check that the prototype, the implementation and the call all match. (I'm guessing without knowing that the prototype is wrong...) If you are calling it from more than one place in the code, the error may originate there as well... The error report should have a line number in it...

    As a matter of practice... you should copy and paste the exact error message.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Ahhh... the next problem, I see...

    Ok, last time was all about process... this time it's looking like efficiency...

    To start you need to work on your text formatting just a little... I generally leave vertical whitespace (blank lines) between functions so they are more clearly blocked out in the code. Not a big deal but it does make it easier to read.

    On the point of efficiency... lines 17, 18 and 19, are using pointers to point to existing variables in the same scope. This is not necessary as you can simply do this in line 22...
    Code:
    par complex(z1, &magnitude, &phase);
    ... eliminating two unnecessary variables from your program... and thus two potential causes of error.

    I don't see the error you've listed from this code snippet... is par_complex() prototyped someplace else? If so check that the prototype, the implementation and the call all match. (I'm guessing without knowing that the prototype is wrong...) If you are calling it from more than one place in the code, the error may originate there as well... The error report should have a line number in it...

    As a matter of practice... you should copy and paste the exact error message.
    Yes, the second problem! Almost done with it! I meant to leave spaces in between the functions, but forgot to. I was wondering if you could do that with those. So, are you saying to get rid of the pointers as a whole? I don't know if you saw my previous reply, but it is working now. I apologize for not copying and pasting the exact error message. I know now though! Thanks for the help!

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yeah, you should leave a blank line or 2 between functions... It's a visual thing, the compiler doesn't care, but it is easier for us mere humans to read.

    Yes, unless you have some other reason for making pointers as you did... ditch them. They contribute nothing to the code.

    Intermediate variables that have no other function are just another source of error.
    General rule... don't make variables you don't absolutely need.


    Glad you got it working... now it's test and improve time!

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Yeah, you should leave a blank line or 2 between functions... It's a visual thing, the compiler doesn't care, but it is easier for us mere humans to read.

    Yes, unless you have some other reason for making pointers as you did... ditch them. They contribute nothing to the code.

    Intermediate variables that have no other function are just another source of error.
    General rule... don't make variables you don't absolutely need.


    Glad you got it working... now it's test and improve time!
    I will make sure to leave spaces from now on! Also, for visual purposes is it better to do the thing where you have the functions at the bottom? I am not sure what it is called. Thanks for the other tips!

    Testing and improving is under way!

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    for visual purposes is it better to do the thing where you have the functions at the bottom?
    Oh boy... did you hear that metallic click just then? Yeah that's called "stepping in it"...

    The whole "functions above" vs "functions below" argument has been done to death over and over again and nobody ever wins...

    Personally --and I do mean "this is just me talking"-- I prefer the functions above method. First it eliminates the need to prototype everything and secondly it enforces a better structure in the way you organize your functions. The compiler reads the page top-down and needs to see a function before it's called, or it will make wild assumptions that will almost always be incorrect... Thus you are required to organize your code so that a function is always seen before it's called, giving a better sense of program flow... I use function prototypes only when I absolutely have to give the compiler a "forward reference" to work with...

    All those wrong people out there (and you know who you are ) will tell you that prototypes with functions below is better because it gives you greater freedom in how you compose and write your code, plus they seem to be under the halucenatory notion that main() should be first...

    So... LOL... the message is, try both and use the way you're most comfortable.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    Oh boy... did you hear that metallic click just then? Yeah that's called "stepping in it"...

    The whole "functions above" vs "functions below" argument has been done to death over and over again and nobody ever wins...

    Personally --and I do mean "this is just me talking"-- I prefer the functions above method. First it eliminates the need to prototype everything and secondly it enforces a better structure in the way you organize your functions. The compiler reads the page top-down and needs to see a function before it's called, or it will make wild assumptions that will almost always be incorrect... Thus you are required to organize your code so that a function is always seen before it's called, giving a better sense of program flow... I use function prototypes only when I absolutely have to give the compiler a "forward reference" to work with...

    All those wrong people out there (and you know who you are ) will tell you that prototypes with functions below is better because it gives you greater freedom in how you compose and write your code, plus they seem to be under the halucenatory notion that main() should be first...

    So... LOL... the message is, try both and use the way you're most comfortable.
    Haha. I like your explanation I didn't realize it was such a controversial issue I messed around with the functions below method a little, but as of right now I prefer them above. I just wasn't sure if it was common practice to do one over the others. Thanks for your opinions! My code seems to work with all cases, there are spaces in between the functions, the pointers are gone, and the outputs look all pretty! I call that a success

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    Haha. I like your explanation I didn't realize it was such a controversial issue I messed around with the functions below method a little, but as of right now I prefer them above. I just wasn't sure if it was common practice to do one over the others. Thanks for your opinions! My code seems to work with all cases, there are spaces in between the functions, the pointers are gone, and the outputs look all pretty! I call that a success
    If you'd like a review... you can post it... I'm sure we'd love to pick it apart ...

    (EDIT: And in case you haven't noticed by now programmers just love to argue...)
    Last edited by CommonTater; 10-17-2011 at 09:44 PM.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by CommonTater View Post
    If you'd like a review... you can post it... I'm sure we'd love to pick it apart ...
    Haha. That sounds scary! I already realized that I probably shouldn't have posted the last code because my professor is really big on catching cheaters. If two codes look too similar the students with those codes get a 0. Anyway, I say that to say that I am kind of scared to post the code just in case a classmate finds it and copies it. Maybe I am paranoid :/ Lol. I might post it once we have all turned it in just to get feedback and see what I should improve

  12. #12
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I've always been a "functions below" man. It's how I was taught, and I've never had a good reason to change.

    Now, who wants to hear me dogmatically state my opinion regarding the One True Indentation Style as though it's incontrovertible fact and that everyone who disagrees is stupid or perverse?
    Code:
    while(!asleep) {
       sheep++;
    }

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    36
    Quote Originally Posted by TheBigH View Post
    I've always been a "functions below" man. It's how I was taught, and I've never had a good reason to change.

    Now, who wants to hear me dogmatically state my opinion regarding the One True Indentation Style as though it's incontrovertible fact and that everyone who disagrees is stupid or perverse?
    Call me stupid...but what exactly is the One True Indentation Style?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by TheBigH View Post
    I've always been a "functions below" man. It's how I was taught, and I've never had a good reason to change.

    Now, who wants to hear me dogmatically state my opinion regarding the One True Indentation Style as though it's incontrovertible fact and that everyone who disagrees is stupid or perverse?
    LOL... and then there's my shockingly unique indentation style... (That even I don't suggest anyone actually use.)

    Code:
    // Get history and settings from the registry
    void LoadSettings(void)
      { HKEY reg;
        DWORD ksize;	
        if (RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\-----\\FSScan",
                          0,KEY_READ,&reg))
           { MessageBox(0,"Unable to load settings","Error", MB_ICONHAND | MB_OK);
              return; }				
        ksize = sizeof(DWORD);
        RegQueryValueEx(reg,"BlockCount",NULL,NULL,(RVAL)&BlockCount,&ksize);
        ksize = sizeof(BOOL);
        RegQueryValueEx(reg,"Recursion",NULL,NULL,(RVAL)&Recurse,&ksize);
        SendMessage(WHandle[5],BM_SETCHECK,Recurse,0);
        for (int x = 0; x < 10; x++)
          { TBuff[0] = 0;
            ksize = sizeof(TBuff);
            sprintf(SBuff,"StartFolder%d",x);
            RegQueryValueEx(reg,SBuff,NULL,NULL,(RVAL)TBuff,&ksize); 
            if (ksize > 0)
              SendMessage(WHandle[2],CB_ADDSTRING,0,(LPARAM)TBuff); } 
        SendMessage(WHandle[2],CB_SETCURSEL,0,0); 
        RegCloseKey(reg); 
        // locate the help file
        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\-----\\FSScan",
                          0,KEY_READ,&reg))
          { MessageBox(0,"Can't find the help file","Error",
                        MB_ICONHAND | MB_OK); 
            return; }
        ksize = MAX_PATH;    
        RegQueryValueEx(reg,"HelpFile",NULL,NULL,(RVAL)&HelpFile,&ksize);
        RegCloseKey(reg); }
    Dogma... Smogma... I say!
    Last edited by CommonTater; 10-17-2011 at 09:52 PM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by COG92 View Post
    Anyway, I say that to say that I am kind of scared to post the code just in case a classmate finds it and copies it.
    Then HE's the cheater, not you...

    And in a highly competative job market that is one way of improving your chances...

    Seriously though... I take your point. One of the thoughts when I posted my solution on that last project was that "Now nobody dares copy it!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-06-2010, 03:18 AM
  2. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  3. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  4. passing argument from incompatible pointer type
    By bgalin in forum C Programming
    Replies: 2
    Last Post: 12-06-2009, 07:14 AM
  5. Replies: 3
    Last Post: 07-17-2007, 03:12 PM