Thread: Beginner-need help

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    17

    Beginner-need help

    hello, i'm a beginner in C programming
    i need some help about C.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        float av,prc,i,j,k;
        scanf("%f %f %f", &i, &j, &k);
        mak(&i,&j,&k,&av,&prc);
        printf("\n%f %f",av,prc);
        return 0;
    }
    
    mak(float a, float b, float c, float *d, float *e)
    {
        *d=(a+b+c)/3;
        *e=*d;
    }
    when i compile it,the compiler said "error: conflicting types for 'mak'."
    and when i change the data type of variables i,j,k,a,b,c from float to integer, the error message shows no more. how can be it happen? what should I do if i still want the type of variables i,j,k,a,b,c is float?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You haven't told the compiler what types the function mak receives, so when you pass &i etc, it will ASSUME that you are passing pointer to float (as i is a float). When you then define the function after main, you say the parameter is of type float (not a pointer). Which is not what the compiler expected.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You need to provide a function prototype or place the code for mak before main. In any case, your function's arguments are either wrong (float versus float pointers for the first three args), or you are calling the function incorrectly (using & when you don't need to for those same first three args).

    [edit]Nevermind, matsp got it.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    17
    i'm sorry, the code that i have posted before is the wrong code before las editing.
    exactly the code like this

    Code:
    int main()
    {
        float av,prc,i,j,k;
        scanf("%f %f %f", &i, &j, &k);
        mak(i,j,k,&av,&prc);
        printf("\n%f %f",av,prc);
        return 0;
    }
    
    mak(float a, float b, float c, float *d, float *e)
    {
        *d=(a+b+c)/3;
        *e=*d;
    }
    as hk_mp5kpdw said, i swap the position of those function, it turn out like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    mak(float a, float b, float c, float *d, float *e)
    {
        *d=(a+b+c)/3;
        *e=*d;
    }
    int main()
    {
        float av,prc,i,j,k;
        scanf("%f %f %f", &i, &j, &k);
        mak(i,j,k,&av,&prc);
        printf("\n%f %f",av,prc);
        return 0;
    }
    amazingly, it works. it makes questions in my mind.
    1.how can just swapping the position of functions makes it works?
    2.and, what's wrong with the first code(before swapping)?why it goes error?
    Last edited by new-b; 05-31-2009 at 08:37 PM.

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Swapping of the functions is no magic. your previous code(without swap) required function prototype declaration for mak because in C the rule is you've to declare the function before you define it. If you're not declaring the function just define it anywhere before it is used. In your previous case the compiler was checking for the declaration and you didn't provide it that's why it showed error. Actually the compiler assumed unknown number of int arguments and that's the reason after you changed them to int it showed no error.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    17
    I see..
    so i must set the prorotype first in main().

    but, how i declare the protoype if the function receives ponter?
    because the last two arguments are pointers.
    should i write "mak(float, float, float, float,float)"?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The prototype for a function is basically the same as the line that declares it:
    Code:
    void foo( int x );  /*prototype*/
    ...
    
    void foo( int x ) /*declaration*/
    {
        ... do stuff ...
    }
    The only real difference is that you don't actually have to name the variables anything in a prototype if you don't want to.


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

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by new-b View Post
    I see..
    so i must set the prorotype first in main().

    but, how i declare the protoype if the function receives ponter?
    because the last two arguments are pointers.
    should i write "mak(float, float, float, float,float)"?
    For pointers declaration is like this.

    int func(int *,float *,int);//the first two arguments are poniters to int and float respectively
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    17
    i declare the function in the same form as BEN10 said, and it works.

    thank you very much for your help.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could have listened to what I said, and you'd know WHY it works.


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

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    17
    so, if i don't declare the function, the compiler will assume the type of function and arguments received by function are always integers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM