Thread: Expected expression error

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    Expected expression error

    Hey there guys! New to cboard. Got tired of the people over at stack overflow, so I'm hoping you guys can help me out. So I'm pretty new to C, and keep getting an error. Basically I'm trying to convert a ppm image to all red. Here is my code that I can't get to work:

    Code:
    change(pixel_t *PIXEL, &w, &h,pixel_t *buffer);
    And here is the beginning of my convert function:

    Code:
    void change(pixel_t *pixel, int w, int h, pixel_t *buffer) {
    
         int average, sum;
         int i;
         pixel_t *pointer;
    Everything else works fine. I keep getting an error when I call the convert function. It says "expected expression before pixel_t" and "too few arguments to function "change".

    I know that everything else in the main is working. I can post more if needed, but don't think it is necessary. I could be wrong though. Does anyone see my problem?

    Sorry if it is super simple and I'm just too dumb to see it.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you call change:
    Code:
    change(pixel_t *PIXEL
    the part after the ( is technically a declaration, the parser wants a pointer type expression.

    You need to allocate a pixel and a pixel buffer, and end up with something like this:
    Code:
    pixel_t pixel;
    pixel_t *buffer = /* allocate buffer */;
    change(&pixel, &w, &h, buffer);
    If there is documentation available, it should explain what change does and how to call it, specifically.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    In the prototype you are essentially passing pointers to w and h instead of the ints you have defined in the actual function.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    From your post it is quite concludable that you lack the understanding of the pointers. There are the concept by its own which may take a while to understand. You must indeed spend some time understand them before you attempt to use or at least at this stage of the learning process.

    In you function prototye 'change' you have pointer variables being declared. That is the variables aything with the '*' prefix. They are all pointers and ofcourse they are different level pointers you can work with. But this one just a single level with just one '*'. These variables can only hold an address of an another varaible. Thats is

    Code:
    int *p;
    int i;
    p = &i;
    There in your case function 'change' first parameters is defined as follow

    pixel_t *PIXEL - Which reveals that it is a pointer, for which its expecting pointer of type pixel_t. When you call the function you need to pass the address of the variable pixel_t which decalred in the calling function somewhere pass it on like as follow

    change( &<variable name>, ...

    I wasn't inclined to write an entier explaination for rest of the function prototype. Try make use of the what i've explained so far to make sense of the rest. Hope this help.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: expected primary expression and ; before else
    By dragonfly22 in forum C++ Programming
    Replies: 5
    Last Post: 09-17-2012, 04:24 PM
  2. error: expected an expression
    By cdmstr in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2011, 02:00 PM
  3. Compiling error expected expression.
    By tasmod in forum C Programming
    Replies: 10
    Last Post: 08-12-2010, 04:26 PM
  4. error: expected expression before ‘{’ token
    By nendariani in forum C Programming
    Replies: 7
    Last Post: 09-14-2008, 11:27 AM
  5. Error: expected an expression!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 01-05-2007, 09:05 AM