Using UNION when porting a Fortran EQUIVALENCE code






3.67/5 (8 votes)
Mar 9, 2003

40702
Using UNION when porting a Fortran EQUIVALENCE code.
Introduction
For those of us who need to port from Fortran:
In C++ there is no equivalent structure to Fortran's EQUIVALENCE
. (No pun intended!)
After doing some searching, I could not find an appropriate code, so I am suggesting the following "work-around" using the UNION
structure.
The basic idea is that the same portion of the memory is accessed by different data. (Variables or data types).
#include <iostream.h> //======================== union mix_it { struct { double a; double b; double c; } s; double x[3]; } mix; //======================= void main() { mix.s.a=12.1; mix.s.b=12.2; mix.s.c=12.3; for (int i=0;i<3;i++) cout<<mix.x[i]<<endl; } //======================
I wish to give credit to this article.