Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

struct with a pointer to a member of another struct? 1

Status
Not open for further replies.

fyrdman

Programmer
Apr 1, 2005
10
US
I have a project where I want to have the two general types of structures:

struct A {

unsigned char *a;

};

struct B {

unsigned char b;

};


The general idea is that I want a member of struct A to be a pointer a member of struct A (without knowledge of the structure of B, other than B has a member unsigned char b).

My question is how do I initialize *a to the address of b? (and then properly reference b through *a within a function?)


I wish to initialize an instance of struct A as follows:

const struct A constinrom = { …, <something that extracts the addr of b> , …};


I wish to use struct A as follows (with no knowledge in anyfunction of struct B other than what is provided by struct A) :

void anyfunction (const struct A *name) {

(set variable c to equal the value in b)

(modify the value of c)

(set value in b equal the value in value of c)

}


Thank you,

Fyrdman


 
*That* link is very helpful, but not yet, as I will have to read it 3-4 times I think.
That one always takes me several passes, too. Maybe I should have started with this one instead.


A good use for [tt]void*[/tt] is for functions like [tt]memcpy[/tt] or [tt]qsort[/tt] or its comparison routine. These are places where no particular type is specified because many types could be used.
 
DaveSinkula,

Maybe something interesting would be cautions of where you can go wrong with void * (or would that be too unbounded of a question to ask of C? )
 
Some notable properties of an object of type [tt]void*[/tt]:[ul]
[li]may point to any object[/li]
[li]can convert any pointer to object or incomplete type to [tt]void*[/tt] and back again and you get the same pointer value back[/li]
[li]is able to represent the values of any pointer type (except pointers to functions) such that those values can be recreated[/li]
[li]initialization and assignements do not require a cast; any necessary conversions are performed implicitly[/li]
[li]cannot perform arithmetic it[/li]
[li]cannot dereference it[/li][/ul]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top