johnreli
johnreli

Reputation: 31

C Struct pointer as Parameter

I'm trying to pass a pointer to a struct in C but i cannot:

float calcular_media(struct aluno *aluno) {

Output warning:

C:\WINDOWS\system32\cmd.exe /c gcc main.c aluno.c
aluno.c:7:29: warning: 'struct aluno' declared inside parameter list

What am I doing wrong? Thank you.

Upvotes: 3

Views: 20912

Answers (5)

Arun
Arun

Reputation: 20393

In the file containing the line

float calcular_media(struct aluno *aluno) {

one of the following must be there before the line

  • struct declaration: e.g. struct aluno; or
  • struct definition: e.g. struct aluno { char c; int i; double d; }; or
  • include of some header file which has one of the above: e.g. #include "aluno.h"

Upvotes: 6

karlphillip
karlphillip

Reputation: 93468

I believe you will end up doing something like this:

#include <stdio.h>
struct aluno
{
  int nota1;
  int nota2;
}

float calcular_media(struct aluno* individuo) 
{
  printf("nota 1:%d\n", individuo->nota1);
  printf("nota 2:%d\n", individuo->nota2);
}

int main()
{
  struct aluno primeiro_aluno;
  primeiro_aluno.nota1 = 9;
  primeiro_aluno.nota2 = 5;

  calcular_media(&primeiro_aluno);

  return 0;
}

Upvotes: 5

pmg
pmg

Reputation: 108988

Do you have a file named "aluno.h" (with the definition of struct aluno) and are you including it in "aluno.c"?

/* aluno.c */
#include "aluno.h"

float calcular_media(struct aluno *aluno) { /* ... */ }

Upvotes: 1

nategoose
nategoose

Reputation: 12392

You need to let the compiler know that there is a struct called aluno before you start passing it to functions.

struct aluno {
   int x;
   int y;
};

float calcular_media(struct aluno * aluno) {
       // ...
}

Upvotes: 1

jsegal
jsegal

Reputation: 1271

Are you declaring struct aluno prior to this function?

Either with a full definition:

struct aluno {
   ...
};

Or at least a forward declaration:

struct aluno;

Upvotes: 5

Related Questions