lefrost
lefrost

Reputation: 581

Getting a warning when passing struct through function

I'm trying to pass a whole structure through a function by value, and I test if the pass was successful by displaying the variables in the structure that was passed.

When I compile and run the program, the variables display correctly, but I get a warning:

warning: parameter names (without types) in function declaration

So it refers to the function prototype being declared with parameter names that had no types? But if I declared the function correctly, why am I getting this warning?

Also, in the short program I wrote to test this, the warning doesn't affect the fact that the output being displayed is correct.

But if I were to get this warning under the same circumstances (passing struct through function) when writing a larger-scale program, will it affect the output in that program being correct?

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void pass(Info);

typedef struct {
    char str[10];
    int num;
} Info;

void main() {
    Info i;

    strcpy(i.str, "Hello!");
    i.num = 1;

    pass(i);
}

void pass(Info i) {
    printf("%s\n", i.str);
    printf("%d\n", i.num);
}

Output:

Hello!

1

Upvotes: 1

Views: 473

Answers (3)

Malik Ji
Malik Ji

Reputation: 339

Because you are declare the function prototype before the Info structure declaration.

An alternative could be the following code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct 
{
    char str[10];
    int num;
} Info;

void pass(Info);

void main() 
{
      Info i;

      strcpy(i.str, "Hello!");
      i.num = 1;
      pass(i)
}

void pass(Info i) 
{
     printf("%s\n", i.str);
     printf("%d\n", i.num);
}

Upvotes: 3

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

The compiler considers this function declaration

void pass(Info);

as a function declaration with an identifier list. That is here Info is identifier of the parameter not its type.

However according to the C Standard (6.7.6.3 Function declarators (including prototypes)):

3 An identifier list in a function declarator that is not part of a definition of that function shall be empty.

SO the compiler issues a warning that the identifier list is not empty for the function declaration that is not a function definition.

warning: parameter names (without types) in function declaration

Either you should write

void pass();

Or you should place the typedef of the structure declaration before the function declaration and in this case the name Info will denote a type.

Upvotes: 2

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

You have the typedef statement after the usage of the newly defined type (synonym for another type, to be nitpicky). At that point, in the function forward declaration, compiler does not know a type named Info. So, it assumes it to be a variable name.

Move the typedef before function prototype.

That said, void main() is not a valid signature for main() in a hosted environment. It should be int main(void), at least.

Upvotes: 3

Related Questions