views:

78

answers:

5

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.

+4  A: 

Are you declaring struct aluno prior to this function?

Either with a full definition:

struct aluno {
   ...
};

Or at least a forward declaration:

struct aluno;
jsegal
+1  A: 

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) {
       // ...
}
nategoose
+1  A: 

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) { /* ... */ }
pmg
+1  A: 

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;
}
karlphillip
Don't forget to vote up my answer or accept it as the official answer if it solved your problem.
karlphillip
+3  A: 

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"
ArunSaha