Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 8/28/84; site lll-crg.ARPA
Path: utzoo!watmath!clyde!cbosgd!ihnp4!mhuxn!mhuxb!mhuxr!ulysses!allegra!mit-eddie!godot!harvard!seismo!ut-sally!ut-ngp!lll-crg!brooks
From: brooks@lll-crg.ARPA (Eugene D. Brooks III)
Newsgroups: net.lang.c
Subject: A useful construct for C ?
Message-ID: <389@lll-crg.ARPA>
Date: Mon, 4-Feb-85 19:42:26 EST
Article-I.D.: lll-crg.389
Posted: Mon Feb  4 19:42:26 1985
Date-Received: Fri, 8-Feb-85 02:14:32 EST
Organization: Lawrence Livermore Labs, CRG group
Lines: 32

References:

I would like to sample the community on a possibly useful construct
	typeof(foo)
which is the type of expression foo.  It is similar in utility to sizeof().

When doing storage allocation with malloc the worst way to do it is:
	int *foo;
	foo = (int *)malloc(sizeof(int) * nelts);

A more sophisticated way of doing it is:
	int *foo;
	foo = (int *)malloc(sizeof(*foo) * nelts);

I like to use the above as I don't have to change the arguments to malloc
if I change the type declaration of foo.  I do have to change the type
cast to the new type however to keep lint happy.

It would be nice to be able to say:
	int *foo;
	foo = (typeof(foo))malloc(sizeof(*foo) * nelts);

In this case, if one changed the declaration of foo then one
would not have to change the executable line.

Yes, I do realize that you can come darn close to this.  You can put the two
lines you have to change together. Which is better than nothing.
	int *foo;
	typedef int *foo_t;
	foo = (foo_t)malloc(sizeof(*foo) * nelts);

But the typeof() construct would not require the typedef declaration!

Is such a typeof() construct available in C?  Should it be?