How do i 'relabel' variables (in fortran)

  • Context: Fortran 
  • Thread starter Thread starter tothetop
  • Start date Start date
  • Tags Tags
    Fortran Variables
Click For Summary
SUMMARY

The discussion focuses on relabeling variables in Fortran, specifically using pointers to enhance code readability. In Fortran 90 and later, developers can utilize pointers to create local references to global variables, similar to the C++ approach. The example provided demonstrates how to declare a target variable and a pointer, allowing for cleaner code without namespace pollution. The syntax used is "integer, target::GauntCoefficients" and "integer, pointer::gc" followed by "gc=>GauntCoefficients".

PREREQUISITES
  • Understanding of Fortran 90 syntax
  • Familiarity with pointers in programming
  • Knowledge of variable scope and namespace management
  • Basic experience with C++ for comparative analysis
NEXT STEPS
  • Research Fortran 90 pointers and their restrictions
  • Explore variable scope management in Fortran
  • Learn about best practices for code readability in Fortran
  • Investigate performance implications of using pointers in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, programmers transitioning from C++, and anyone looking to improve code readability and maintainability in Fortran applications.

tothetop
Messages
2
Reaction score
0
Suppose I have a global variable like "GauntCoefficients".. and I want to use it in a formula in the next block of code. In C++, I might do something like this to make the code more readable:

const int* gc = GauntCoefficients;
(then write some nasty formula with "gc" rather than "GauntCoefficients" everywhere)

Since "gc" is declared here as a constant, the compiler should just delete it (I think). And since "gc" id declared locally.. it doesn't pollute the namespace.

Can I do something like that in Fortran?
 
Technology news on Phys.org
Hey
In Fortran 90 and newer pointers are possible to use. But they are more restrictive than in e.g. C/C++.
You can therefore do something like this:
Code:
integer, target::GauntCoefficients
integer, pointer::gc
gc=>GauntCoefficients

I hope this helps.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
1K