Address of struct in struct


B

Borealis

Hi,

this might be a silly question but I really got stuck here.

Suppose I have:

struct A {
int a;
int b;
int c;
};

struct B {
int x;
int y;
struct A z;
}

struct B params[2];

void doSomethingWithParams(struct B* arg)
{
/* Do something with parameters */
}

void main(void)
{
doSomethingWithParams(params[1].&z);
}

The function-call in main does not work. I get an error: expected a
field name

Is this a limitation of the c language? Is it not possible to get the
address of struct in struct? What would be a alternative to this? I
know I could have static variables for z and assign it as a pointer to
params (with struct A* z in B). But this does not hold relevant
information at one point then...

Thanks for your tips and hints!
 
Ad

Advertisements

T

Tom St Denis

Hi,

this might be a silly question but I really got stuck here.

Suppose I have:

struct A {
  int a;
  int b;
  int c;

};

struct B {
  int x;
  int y;
  struct A z;

}

struct B params[2];

void doSomethingWithParams(struct B* arg)
{
  /* Do something with parameters */

}

void main(void)
{
  doSomethingWithParams(params[1].&z);

&params[1].z

Is what you're after.

Tom
 
J

Jens Thoms Toerring

Tom St Denis said:
this might be a silly question but I really got stuck here.

Suppose I have:

struct A {
  int a;
  int b;
  int c;
};

struct B {
  int x;
  int y;
  struct A z;
}

struct B params[2];

void doSomethingWithParams(struct B* arg)
{
  /* Do something with parameters */

}

void main(void)
{
  doSomethingWithParams(params[1].&z);
&params[1].z

Is what you're after.

But note that this gives you the address of the struct A within
struct B (and not the address of a struct B), so the doSomething-
WithParams() should have a prototype of

void doSomethingWithParams(struct A* arg);

Regards, Jens
 
Ad

Advertisements

B

Borealis

this might be a silly question but I really got stuck here.
Suppose I have:
struct A {
  int a;
  int b;
  int c;
};
struct B {
  int x;
  int y;
  struct A z;
}
struct B params[2];
void doSomethingWithParams(struct B* arg)
{
  /* Do something with parameters */
}
void main(void)
{
  doSomethingWithParams(params[1].&z);
&params[1].z
Is what you're after.

But note that this gives you the address of the struct A within
struct B (and not the address of a struct B), so the doSomething-
WithParams() should have a prototype of

void doSomethingWithParams(struct A* arg);

                              Regards, Jens


That's right, yes. Typo... Thanks for bringing me back on track!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top