Anonymous Structures in C found in Unix Kernel -
i have started reading lions commentary on unix v6. came across these snippets, have never seen used in c language. author provide sort of explanation, explain me happening here?
params.h
:
sw 0177570 ...... struct { int integ; };
and used in unix/prf.c
if(sw->integ == 0)
explanation author
sw
defined value 0177570. kernel address of read processor register stores setting of console switch register. meaning of statement clear: contents @ location 0177570 , see if zero. problem express in c. codeif (sw == 0)
not have conveyed meaning.sw
pointer value should dereferenced. compiler might have been changed acceptif (sw-> == 0)
stands, syntactically incorrect. inventing dummy structure, elementinteg
, programmer has found satisfactory solution problem.
my question how work? when compiler sees sw->integ
, how associate sw
anonymous structure?
iirc, ancient c compilers kept field names (such integ
) in single namespace instead of creating namespace per struct
type. did not distinguish between struct
pointers , int
pointers, every pointer has integ
field corresponding first sizeof(int)
bytes. since integ
first value in struct
, has type int
, sw->integ
corresponds *((int *)sw)
.
Comments
Post a Comment