开发者

C Struct pointer as Parameter

I'm trying to pass 开发者_StackOverflowa 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.


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"


Are you declaring struct aluno prior to this function?

Either with a full definition:

struct aluno {
   ...
};

Or at least a forward declaration:

struct aluno;


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;
}


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) {
       // ...
}


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) { /* ... */ }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜