2

I'm having trouble passing a structure array as a parameter of a function

struct Estructure{
 int a;
 int b;
};

and a function

Begining(Estructure &s1[])
{
   //modifi the estructure s1
};

and the main would be something like this

int main()
{
  Estructure m[200];
  Begining(m);
};

is this valid?

5 Answers 5

1

No, you need to typedef your struct, and you should pass the array to it; pass by reference does not work in C.

typedef struct Estructure{
 int a;
 int b;
} Estructure_s;

Begining(Estructure_s s1[])
{
   //modify the estructure s1
}

int main()
{
  Estructure_s m[200];
  Begining(m);
}

Alternatively:

struct Estructure{
 int a;
 int b;
};

Begining(struct Estructure *s1)
{
   //modify the estructure s1
}

int main()
{
  struct Estructure m[200];
  Begining(m);
}
Sign up to request clarification or add additional context in comments.

Comments

0
Begining(struct Estructure s1[])
{
   //modifi the estructure s1
};

int main()
{
  struct Estructure m[200];
  Begining(m);
  return 0;
};

Comments

0
typedef struct{
 int a;
 int b;
} Estructure;

void Begining(Estructure s1[], int length)
//Begining(Estructure *s1)  //both are same
{
   //modify the estructure s1
};

int main()
{
  Estructure m[200];
  Begining(m, 200);
  return 0;
};

Note: Its better to add length to your function Beginning.

Comments

0

Either stick the work struct in front of Estructure or typedef it to some other type and use that. Also pass by reference doesn't exist in C, pass a pointer instead if you wish. Perhaps:


void Begining(struct Estructure **s1)
{
   s1[1]->a = 0;
}

Not quite the same as an array, but this should work in C land and it passes a pointer for efficiency.

Comments

0

typedef struct { int a; int b;} Estructure;

void Begining(Estructure *svector) { svector[0].a =1; }

1 Comment

i keep getting the following error: tp1.c:96: error: expected declaration specifiers or ‘...’ before ‘Medico’

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.