Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29279 Discussions

Why character scalar variable association with character dummy array is not allowed in fortran95 and why it is allowed in 2003?

tyagi-amit
Beginner
916 Views

Hi All

I am trying to enhance my current compiler which is currently not supported the feature which is newly add in the fortran version 2003.

Fortran 95 feature:
If the actual argument is scalar, the corresponding dummy
argument shall be scalar unless the actual argument is an
element of an array that is not an assumed-shape or pointer
array, or a substring of such an element.

Fortran 2003 feature:
If the actual argument is scalar, the corresponding dummy argument
shall be scalar unless the actual argument is of type default
character, or is an element or substring of an element of an array that is not an
assumed-shape or pointer array.

Example:

program main
IMPLICIT NONE
CHARACTER(kind=1)::a(3)
CHARACTER(kind=1)::b
a=(/"a","b","c"/)
b="a"

CALL sub(a(1)) ! PASS Allowed in fortran 95 and fortran 2003
CALL sub(b) ! FAIL Not allowd in fortran 95 but allowed in fortran 2003

CONTAINS

SUBROUTINE sub(arg_of_sub)
CHARACTER(kind=1), INTENT(in) :: arg_of_sub(3)
Write(*,*) arg_of_sub
END SUBROUTINE sub

END program main

I think its all related to the sequence association.

Just want to know the reason why fortran 95 not allowed the character scalar to character dummy array association.
and how fortran 2003 handle this association...means just want to know the implementation difference in handling the sequence association.

Thanks in advance....!!!!

0 Kudos
4 Replies
Steven_L_Intel1
Employee
916 Views
This is part of the C interoperability support in Fortran 2003. The only type of CHARACTER variable that is "interoperable" is an array of CHARACTER(1) values. This relaxation of the rule for argument matching was made so that you could pass a character string to an interoperable routine where the argument was declared as a character array. The sequence association is handled in the expected way - first character associated with the first element, second with second, etc.
0 Kudos
tyagi-amit
Beginner
916 Views

Thanks a lot Steve for your description.....

I understood that this feature is allowed in fortran 2003 to make interoperable with C
like (char to char*) and follow the sequence association.


But my concern is, if I allow this feature in my fortran compiler which is currently supported f95 features...will it effect any other phase/s.

Because whenever a rule is breaked it effects something that's why the rule always created. so just want to know why this rule(char to char arrray) is not allowed in fortran95.

Is it becuase in fortran 95 there is no meaning to allow association char(scalar) to char array even if it is not breaking the sequence association rule. and fortran2003 has meaning for char to char array that's why it is allowed.

Am i right..?



Please clearify my queries.
Thanks in advance...

Regards
Amit
0 Kudos
Steven_L_Intel1
Employee
916 Views
So you're writing your own compiler?

The previous rule was in place because of the general Fortran rule that actual and dummy arguments must match in type, kind and rank. Passing a scalar to an array is generally disallowed, other than passing a single element of a non-pointer array where you get sequence association.

With the addition of the C interoperability features, it was clear that the only kind of character variable that is interoperable is an array of single characters, otherwise some sort of length information needs to be passed, and interoperable routines forbid that. But that would make passing character values to a C routine inordinately difficult, so they carved out an exception for character values this way.

You will generally find that Fortran 2003 relaxes many of the restrictions of Fortran 95, and Fortran 2008 relaxes even more. Writing a good Fortran compiler is complicated and you need to be aware of the details of the standard.
0 Kudos
tyagi-amit
Beginner
916 Views
Thanks all for your valuable descriptions...
0 Kudos
Reply