简体   繁体   中英

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.

In the file containing the line

float calcular_media(struct aluno *aluno) {

one of the following must be there before the line

  • struct declaration : eg struct aluno; or
  • struct definition : eg struct aluno { char c; int i; double d; }; struct aluno { char c; int i; double d; }; or
  • include of some header file which has one of the above: eg #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) { /* ... */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM