tags:

views:

136

answers:

2

Hi,

I get this error:

transform.c:23: warning: ‘struct user_data_s’ declared inside parameter list
transform.c:23: warning: its scope is only this definition or declaration, which is probably not what you want

Which I think is because I have a struct that contains a struct.

This is what I am trying to do:

void f2(struct user_data_s* data) {
  printf("Number %i\n", data->L);
}

void f1(struct user_data_s* data) {
  printf("Number %i\n", data->L);
  f2(data);
}

The printf in f1 works, but the line

void f2(struct user_data_s* data) {

gives the error.

Does anyone know how I can fix this?

Hugs, Louise

+4  A: 

You have declared your struct in between (or possibly after) your declarations of f2 and f1. Move your struct declaration so that it comes before both declarations.

That is to say:

struct user_data_s
{
    int L;
};

void f2(struct user_data_s* data) {
      printf("Number %i\n", data->L);
}

void f1(struct user_data_s* data) {
      printf("Number %i\n", data->L);
        f2(data);
}

compiles without errors, but

void f2(struct user_data_s* data) {
      printf("Number %i\n", data->L);
}


struct user_data_s
{
    int L;
};

void f1(struct user_data_s* data) {
      printf("Number %i\n", data->L);
        f2(data);
}

will not compile, because f2 has no way to know what a struct user_data_s is.

You might be used to programming in a higher-level language that lets you place your declarations/definitions pretty much anywhere (such as C# or Python), but unfortunately, C is compiled strictly top-to-bottom.

Mark Rushakoff
Thank you very much =) I hadn't included in header file, where the struct were defined. I would never have thought of that my self.
Louise
+1  A: 

The compiler already gave you a pretty good explanation of what's going on.

You haven't declared struct user_data_s anywhere in advance. The compiler sees struct user_data_s in the above function definitions for the very first time ever. In each case the declaration of struct user_data_s has block scope, i.e. it is local to the corresponding function. This means that the first declaration of struct user_data_s in the f1 definition is completely unrelated the the second declaration of struct user_data_s in f2 definition. These declarations declare two completely different local (to each function) types struct user_data_s. You can't call f2 from f1 as in your example since their parameter types are completely unrelated.

Normally, you should have struct user_data_s declared at file scope in advance, instead of declaring it in function definition. Did you forget to include a header with the declaration of struct user_data_s by any chance?

AndreyT
Yes =) That was exactly the problem. Forgot to include the header file =) Thank you very much for the in depth explanation. Now I will never make that mistake again =)
Louise