C Struct pointer as Parameter

18,084

Solution 1

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"

Solution 2

Are you declaring struct aluno prior to this function?

Either with a full definition:

struct aluno {
   ...
};

Or at least a forward declaration:

struct aluno;

Solution 3

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

Solution 4

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

Solution 5

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) { /* ... */ }
Share:
18,084
johnreli
Author by

johnreli

Updated on June 05, 2022

Comments

  • johnreli
    johnreli almost 2 years

    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.

    • tidwall
      tidwall over 13 years
      You will need to post more code.
    • David R Tribble
      David R Tribble over 13 years
      The compiler error says it all: the struct is not defined prior to the function definition.