maty_nz
maty_nz

Reputation: 280

passing an array structure as an array

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?

Upvotes: 2

Views: 194

Answers (5)

renick
renick

Reputation: 3881

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

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

Upvotes: 0

Michael Dorgan
Michael Dorgan

Reputation: 12515

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.

Upvotes: 0

WhirlWind
WhirlWind

Reputation: 14112

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

Upvotes: 1

N 1.1
N 1.1

Reputation: 12524

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.

Upvotes: 0

N 1.1
N 1.1

Reputation: 12524

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

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

Upvotes: 0

Related Questions