Click here to Skip to main content
Click here to Skip to main content

Using UNION when porting a Fortran EQUIVALENCE code

, 8 Mar 2003
Rate this:
Please Sign up or sign in to vote.
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

About the Author

Gabor Kalman
Web Developer
United States United States
Retired EE

Comments and Discussions

 
GeneralFor those with the need... PinmemberNitron11-Mar-03 14:25 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 9 Mar 2003
Article Copyright 2003 by Gabor Kalman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid