[TUHS] v7 K&R C

Michael Kjörling michael at kjorling.se
Sun Apr 26 06:07:03 AEST 2020


On 25 Apr 2020 19:01 +0000, from blstuart at bellsouth.net (Brian L. Stuart):
>  On Saturday, April 25, 2020, 09:52:45 AM EDT, Hellwig Geisse <hellwig.geisse at mni.thm.de> wrote:
>> The subject can be looked at from another angle. Consider
>> the call f(42). This might be read as first naming f (and
>> thus constructing a pointer to f) and then calling the
>> function which the pointer is pointing to.
> 
> This is the way that I've taken to looking at it for the
> last 10 years or so. In fact, I see it as the same thing
> as an array. Specifically, I've taken to thinking of []
> as a postfix indexing operator and () as a postfix
> calling operator, and the thing on the left is a pointer
> in both cases.

That's an interesting way of looking at it.

I was thinking: couldn't we apply the same kind of reasoning to
variables as well?

Bear with me for a second.

If we have

  int z = 123;

then "z" is a mnenomic way of referring to an int-sized memory
location, which after initialization holds the value 123. In C, we can
take the address of any variable stored in memory, and we can
dereference any address into memory. (How _meaningful_ especially the
latter is varies, particularly on memory-protected architectures, but
it's still possible.)

So, is there any material difference between

  printf("%d", z);

and

  printf("%d", *(&z));

If there is, then certainly GCC isn't indicating that it's there. Both
print 123, and both variants compile cleanly even with -Wall -pedantic.

OpenBSD clang 8.0.1 cc also gives identical output for both variants.

So if "z" and "*(&z)" (take the address of z, then dereference that
address, then use the value stored there) are equivalent, then in the
name of consistency, why _shouldn't_ f() and (*f)() (dereference the
address of f, then call) also be equivalent? After all, what is "f"
here, other than a mnenomic name for a memory location?

-- 
Michael Kjörling • https://michael.kjorling.semichael at kjorling.se
 “Remember when, on the Internet, nobody cared that you were a dog?”



More information about the TUHS mailing list