- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
This might be a stupid question but I just found a bug in something I wrote earlier today and I was surprised the complier didn't have a little moan at me.
character(12), parameter :: gudef(5)=(/'@@USER-DEF-1@@','@@USER-DEF-2@@','@@USER-DEF-3@@','@@USER-DEF-4@@','@@USER-DEF-5@@'/)
Those 14 character long strings don't fit that well in the 12 character spaces...
It would whinge at me if they weren't all this same length but all the wrong length is OK?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Since all the elements are of the same length, one can now do:
character(len=*), parameter :: gudef(5) = [ '@@USER-DEF-1@@', '@@USER-DEF-2@@', &
'@@USER-DEF-3@@', '@@USER-DEF-4@@', &
'@@USER-DEF-5@@' ]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
And if the elements are of different length, the following makes it easier:
character(len=*), parameter :: s(3) = [ character(len=6) :: "foo", "bar", "foobar" ]
The type-def in the array expression has to specify the longest length expected, of course.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The standard says, "The entity has the value specified by ts constant-expr, converted, if necessary, to the type, type parameters and shape of the entity." The rules of such conversion include truncation of character strings. I agree that it would be nice for the compiler to warn you about this - I will suggest it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks FF #2 is what I should have done, I tend to code such things on autopilot and adding the length rather than * in this instance was one of those. I wasn't aware of the syntax of #3 and yes that is quite useful also but not for the current activity.
Steve, thanks not a high priority I know but as you say it would be nice because I think 19 times out of 20 it would expose a code bug at compile time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
FortranFan wrote:
And if the elements are of different length, the following makes it easier:
character(len=*), parameter :: s(3) = [ character(len=6) :: "foo", "bar", "foobar" ]The type-def in the array expression has to specify the longest length expected, of course.
I'm intreasted in the sytax here. Could you please suggest a reference to look into it ? Is there any defference between (/ & /) AND [ & ] ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Blane J. wrote:
..
I'm intreasted in the sytax here. Could you please suggest a reference to look into it ? Is there any defference between (/ & /) AND [ & ] ?
See the references in Dr Fortran's blog: https://software.intel.com/en-us/blogs/2013/12/30/doctor-fortran-in-its-a-modern-fortran-world. For the point in this thread, consult Metcalf et al., "Modern Fortran Explained".
In array constructors, you can now pretty much use [ .. ] instead of (/ .. /); Metcalf et al, explains this in more detail.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I use array constructors [..] in code all the time these days but in declarations assignments use (/.../). Am I correct in saying that until quite recently ifort did not accept [....] in declaration initialisation assignments?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
We have accepted [] as alternatives to (/ /) "forever", long before it became standard in F2008 and including in initialization expressions.
Being able to specify the type in an array constructor was in F2003.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Steve Lionel (Intel) wrote:
We have accepted [] as alternatives to (/ /) "forever", long before it became standard in F2008 and including in initialization expressions.
Being able to specify the type in an array constructor was in F2003.
Interesting maybe when I last did that I goofed in some other way and incorrectly assumed that the syntax was not supported in that context....
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page