Click here to Skip to main content
15,892,537 members
Articles / Programming Languages / C++
Article

Using UNION when porting a Fortran EQUIVALENCE code

Rate me:
Please Sign up or sign in to vote.
3.67/5 (8 votes)
8 Mar 2003 40.5K   12   2
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Retired EE

Comments and Discussions

 
Generalvery good trick Pin
Southmountain20-Oct-20 5:34
Southmountain20-Oct-20 5:34 
GeneralFor those with the need... Pin
Nitron11-Mar-03 14:25
Nitron11-Mar-03 14:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.