Pointer Problems

Henry Spencer henry at zoo.toronto.edu
Thu Aug 16 01:34:13 AEST 1990


In article <8aliOrS00WBMI1UVYX at andrew.cmu.edu> gh1r+ at andrew.cmu.edu (Gaurang Hirpara) writes:
>Now, I have another struct, which contains in it a generic 
>pointer (i.e. Ptr <pointername>). 

Uh, what's a "generic pointer"?  There is no such thing in C.

>How can I make <pointername> point to idiot, AND be able to access the 
>resulting pointer as a pointer to struct...

You have to cast the pointer to the desired type every time.  The type of
any C expression, i.e. your pointer, has to be known at compile time, and
that means you can't just use it as "pointer to whatever".  It has to be
a pointer to something specific.

Assuming you use "void *" as your underlying pointer type, the code looks
like this:

	struct foo {int a, b; } f;
	void *vp;

	vp = &f;
	printf("Here's f.a: %d\n", ((struct foo *)vp)->a);
-- 
It is not possible to both understand  | Henry Spencer at U of Toronto Zoology
and appreciate Intel CPUs. -D.Wolfskill|  henry at zoo.toronto.edu   utzoo!henry



More information about the Comp.lang.c mailing list