Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@zoo.toronto.edu (Henry Spencer)
Subject: Re: Pointer Problems
Message-ID: <1990Aug15.153413.24472@zoo.toronto.edu>
Organization: U of Toronto Zoology
References: <8aliOrS00WBMI1UVYX@andrew.cmu.edu>
Date: Wed, 15 Aug 90 15:34:13 GMT

In article <8aliOrS00WBMI1UVYX@andrew.cmu.edu> gh1r+@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@zoo.toronto.edu   utzoo!henry
