What's wrong, invalid argument type unary *

Discussion in 'Mac Programming' started by cybrscot, Dec 26, 2010.

  1. cybrscot macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #1
    I'm not sure what's wrong here, obviously it all looks good to me, but it isn't working. Any help? The compiler errors are posted below the code

    #include <stdio.h>

    main ( )
    {

    int r = 10 ;
    float _π = 3.14 ;
    float volume = 4.0/3.0*π*r^3 ;

    printf ("Volume of a sphere: %.2f\n" , volume) ;

    return 0 ;
    }

    Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
    /Users/scottdean/documents/sphere.c: In function ‘main’:
    /Users/scottdean/documents/sphere.c:7: error: stray ‘\317’ in program
    /Users/scottdean/documents/sphere.c:7: error: stray ‘\200’ in program
    /Users/scottdean/documents/sphere.c:8: error: stray ‘\317’ in program
    /Users/scottdean/documents/sphere.c:8: error: stray ‘\200’ in program
    /Users/scottdean/documents/sphere.c:8: error: invalid type argument of ‘unary *’
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    It look like you may have written this in an editor that doesn't stick to low ASCII. Try saving as plain text or using another editor that allows that.

    ^ is not exponentiation, it is binary XOR.

    Also, there is no n, there is _n. I wouldn't name things with a leading underscore, this is generally "reserved".

    -Lee
     
  3. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    It looks like you got some strange characters in your source code. How did you enter this code into XCode?
     
  4. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #4
    Thanks for helping,

    I'm using textwrangler for my editor, which is supposed to be pretty good.

    I had to search the web to find out how to type an exponent, "they" said that ^ was exponent So I typed r^3

    Also, I needed to type PI, or 3.14, the web said that Alt P, was PI, which it does look like the PI symbol in my editor, but when I copy/paste here it looks like the letter n. I tried to type it here π, but as you can see, it still looks like the letter n, not the PI symbol.
     
  5. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #5
    I just replaced the PI symbol with the letter p instead as a substitute, it changed my compiler errors, seems to be less. Here is the new code and the errors.

    #include <stdio.h>

    main ( )
    {

    int r = 10 ;
    float p = 3.14 ;
    float volume = 4.0/3.0*p*r^3 ;

    printf ("Volume of a sphere: %.2f\n" , volume) ;

    return 0 ;
    }



    Scott-Deans-MacBook-Pro:documents scottdean$ gcc ~/documents/sphere.c
    /Users/scottdean/documents/sphere.c: In function ‘main’:
    /Users/scottdean/documents/sphere.c:8: error: invalid operands to binary ^ (have ‘double’ and ‘int’)
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Don't try to use anything fancy. Name the variable pi, don't try to use the symbol for pi. It is illegal in C source and has no special meaning.

    ^ may be exponentiation in some languages, but not C. It is a binary operator, but won't do what you want at all.

    -Lee
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    Change r^3 to r*r*r. ^ is not exponentiation. The left operand in this case will be a double, ^ only works on integer types. And it's not exponentiation.

    -Lee
     
  8. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #8
    Thanks again Lee, but how can I indicate an exponent in code? Must I just to (4)(4)(4) instead of 4 to the 3rd power?
     
  9. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #9
    Gotcha, we must have been typing at the same time. I didn't see your answer yet for the exponent before I posted again.

    Thanks so much!
     
  10. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #10
    This may sound like a stupid question, not that the others didn't for you pro's, but does the computer know the correct order of operations?
     
  11. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #11
    everything worked good! The PI symbol and the ^ were the problems,

    "adding to my knowledge everyday"

    This is fun, but it's especially rewarding when after getting stuck on a problem, I finally get it to work, and they're just some small problems that I understand. I'm happy there wasn't something inherently wrong with the way I wrote my code. A couple rogue symbols I can deal with.
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    They don't know anything but what we tell them to do.

    There is an order of operator precedence for C.
    http://www.difranco.net/cop2220/op-prec.htm

    That lists the precedence and associativity for all of the operators. When in doubt, parenthesize.

    -Lee
     
  13. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #13
    The computer knows nothing. The C compiler knows the correct order of operations according to the rules of the C Standard. And you will have to learn the same rules. I think you really need to get a good C book.
     
  14. cybrscot thread starter macrumors 6502

    cybrscot

    Joined:
    Dec 7, 2010
    Location:
    Somewhere in Southeast Asia
    #14
    I've got a very good C book. C Programming by K.N. King. Was highly recommended by many. But I'm just doing the exercises at the end of chapter 1. Ch. 1 didn't discuss exponents or PI symbols. So I searched on the web how to type them with the keyboard.

    When all that fails I have to ask you guys. I have no teacher, teaching myself. Here in Vietnam, there are no courses to learn programming, or I'd gladly enroll even if they don't speak English!

    Trust me, I appreciate all your help, and I only ask after I can't find the answer searching Google and reading 3-4 different posts that lead me nowhere. I have spent a couple hours on this problem alone, had no idea C couldn't read those characters, just went to learn how to make them with the keyboard and did it. I didn't think the character would matter, whether I used any letter for a variable, or a symbol, whatever, as long as I defined the variable. But now I know!!
    :)
     
  15. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #15
    For exponentiation, there are a couple of options.

    #1, <math.h> has a function
    double pow(double x, double y);
    Which raises x to the power y

    This is known to be slower for whole powers like pow(2.0, 3.0);
    So if you are only usuing whole powers I'd suggest you make your own function like this
    #2,
    Code:
    // pre: y>=0
    double myIntPow(double x, int y) {
        double temp = 1.0;
        for ( ; y > 0; y--) {
            temp *= x;
        }
        return temp;
    }
    
    Oh and <math.h> has a constant M_PI which is Pi in as much precision as needed for a double (presumably). (has a lot of other M_something for lots of other constants)
     
  16. dmi macrumors regular

    Joined:
    Dec 21, 2010
    #16
    That's probably because C does not have an exponentiation operator or a π symbol, although <math.h> does have a pow function.
    Some versions of <math.h> may also have a M_PI macro, but this is non-standard, so you should not depend on it.

    If y can be large, it may be more efficient to do it more like:
    Code:
        if( y<0 ){ y = -y; x = 1.0/x; }
        for( ; y > 0; y>>=1 ){
            if( y&1 ){ temp *= x; }
            x *= x;
        }
    
     
  17. dcr macrumors member

    Joined:
    Jun 10, 2002
    #17
    Use the pow() function and be happy. Libraries are well-tested and generally will perform better than something you can write yourself. Some compilers even recognize calls to the standard library and inline them with very optimized versions.

    Only write your own version if you've run your software through a profiler and identified the power function as the most important performance problem and you have time to test it thoroughly for correctness.
     
  18. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #18
    I once believed that too, however someone once raised the issue with pow based on the method pow uses to solve (mainly that it doesn't require the power to be a whole number so it uses a somewhat convoluted way of solving)

    So one time I wrote a test program to test it out (in C obviously). If memory serves me, it was similar to x*x for pow(x, 2.0) probably because of some internal check for 2.0 and just the function call overhead. However x*x*x was significantly faster than pow(x, 3.0) and it got worse from there.
     

Share This Page