0

So I have a structure like this:

struct state {
  int previous[2];
  int current[2];
  bool pen;
}; 
typedef struct state state;

In a few functions I used this as an argument, for example:

void new_state(&s, char *file, int i, int j){
    int new = s -> current[j];
    s -> current[j] = operand(byte(i, file)) + s -> current[j];
    s -> previous[j] = new;
}

I call these in a function, where I define s as being from state:

void main_function(char *file){
  state s;
  display *display = newDisplay(file, 200, 200);
  int j = size(file);
  for(int i=0; i<j; i++){
    if(opcode(byte(i, file)) == 0){
       new_state(&s, file, i, 0);
    }
    else if(opcode(byte(i, file)) == 1){
      new_state(&s, file, i, 1);
      draw(s, display, file);
    }
    else if(opcode(byte(i, file)) == 2){
      pause(display, operand(byte(i, file)) * 10);
    }
     else{
        new_pen(s);
    }
    end(display);
}
}

However when compiling I still get the error message: expected declaration specifiers or ‘...’ before ‘&’ token, but I don't understand why. I've defined the variable s as being part of the structure state and then by using the &s, this gives it the address to follow right?

  • 2
    You get it because `void new_state(&s, char *file, int i, int j)` is complete gibberish. A function declaration is not a function call. – Lundin Dec 18 '17 at 10:42
  • Note that the dot `.` and arrow `->` operators bind very tightly indeed and should not have spaces around them, unlike lower-precedence binary operators such as `<` which should have spaces around them. Yes, it is stylistic. Spaces around dot and arrow are definitely not common except in novice code. – Jonathan Leffler Dec 18 '17 at 11:24

3 Answers3

1

void new_state(&s, is wrong.

In C it should read

void new_state(state *s, char *file, int i, int j)
Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

You must distinguish between a function definition and a function call. When defining a function with parameters, you must provide the data type of each parameter, unless it is the variable argument list ... .

When calling the function, however, you should pass a constant or a variable of a compatible type.

machine_1
  • 4,266
  • 2
  • 21
  • 42
0
void new_state(&s, char *file, int i, int j){
    int new = s -> current[j];
    s -> current[j] = operand(byte(i, file)) + s -> current[j];
    s -> previous[j] = new;
}

this in your code is the function definition. struct state is a data type. s is just a variable of the type struct state. Also &s (in the scope in which s is defined) refers to the address of s in the memory.

what you really wanna do here is make a function that takes a pointer to any variable of type struct state. the correct syntax for that would be

void new_state(struct state * s, char * file, int i, int j){
    ...(function body)...
}

now, in the function call, (that's when you use a function in the main or elsewhere). you kinda put something you have already made to use.

the declaration is general(to be used for any inputs). the call, however is specific (or particular).

here you specify the parameters(or arguments) that you are passing to the function.

this is what the call would look like

.
.
.
new_state(&s, file, i, j);
.
.
Parth K
  • 587
  • 5
  • 18