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. code if (sw == 0) not have conveyed meaning. sw pointer value should dereferenced. compiler might have been changed accept if (sw-> == 0) stands, syntactically incorrect. inventing dummy structure, element integ , 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

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -