Path: csiph.com!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Spurious diagnostic messages Date: Mon, 20 Jul 2015 15:51:01 -0700 Organization: None to speak of Lines: 61 Message-ID: References: <874mn5ty3y.fsf@kst-u.example.com> <87egm8qis7.fsf@kst-u.example.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="21622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MIpcP4YeQES8pFwzeNoc+" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:RUypwX62Vw3WQEoVf59ii4Jb+vw= sha1:jM+QNyGjCck5Mx3noiijIMamRM8= Xref: csiph.com comp.lang.c:66077 Bartc writes: > On 20/07/2015 19:52, Keith Thompson wrote: >> Bartc writes: [...] >>> Yet just a couple of days ago I was debugging something and I need to >>> print out HWND and UINT values, when I had to pause; were they 32-bits >>> or 64-bits? I had to take a guess and hope it worked. >> >> You didn't *have* to guess. You could have converted to a 64-bit type >> and printed that value. >> >> HWND is a pointer type; > > Isn't it supposed to be opaque? Yes you can trawl through the docs, and > find that HWND is a typedef for HANDLE, which is a typedef for PVOID, > which is a typedef for void*. But you shouldn't need to. There are also > dozens of the things. I don't know; I haven't done much Windows programming. If it's supposed to be opaque, then you're violating that opacity by printing its value; you can't really expect to have a clean and convenient way to do that. If you don't want to take advantage of the fact that it's (implemented as) a pointer, you *could* memcpy the value of the HWND object to an array of unsigned char of the same size, and print the elements in hex. That's the most general way to print the contents of an object. > I think it's safe to assume that UINT is an >> unsigned integer type. >> >> HWND h = ...; >> UINT u = ...; >> printf("h = %p, u = %lu\n", (void*)h, (unsigned long)u); > > OK, so we already specify type info for h and u in their declarations, > then specify type info in the format string, and here were specifying a > third lot with the casts! The "third lot" is not specifying the types of h and u; it's specifying the type they're converted to for printing. But yes, you have to know a thing or two to be able to determine the most appropriate type, and what to do with the converted result once you have it. I'm not saying this is a model of programming elegance and intuitively obvious abstraction. I'm saying that this is the way you can do it in C. Sure, it's ugly. Sure, it can be done more cleanly in some other languages. Sure, one of those languages could be some hypothetical future version of C with just a few new features added (though the design of those features would be non-trivial). I'm not defending C, I'm just describing it. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"