From: ivan martin on
Is it possible to transfer a string constant (I'm not sure if this is the exact terminology)
to a subroutine ?

What I'm trying to accomplish is something akin to this (fortran pseudo code);
*******
call my_subroutine('string constant',number1)
end

subroutine my_subroutine(string,number1)
character(90) string
real number1

write(*,'(a)')string !why is this not working ?
return
end
*******

Why do I need this when I could simply load the "string constant" from the console, using READ
statement ?
I usually use two programs, one for generating an input file, and the other for processing that
file. So, instead of reading the "string constant" from the user (with READ), I'm trying just to
be able to enter the name of the test and loading and so on ... directly from the program.
But when I try to run the above program, although it compiles relatively ok (one warning), it writes
out garbage (some machine code which I cannot copypaste in here; but can be considered garbage).

Please, any ideas. Where am I going wrong ?
-- Ivan
From: dpb on
ivan martin wrote:
> Is it possible to transfer a string constant (I'm not sure if this is the exact terminology)
> to a subroutine ?

Of course...

> What I'm trying to accomplish is something akin to this (fortran pseudo code);
> *******
> call my_subroutine('string constant',number1)
> end
>
> subroutine my_subroutine(string,number1)
> character(90) string
> real number1
>
> write(*,'(a)')string !why is this not working ?
> return
> end
> *******
>
....

Mismatch in length as written -- use assumed length in the subroutine
instead.


character*(*) :: string

and the local copy will take the length of the actual string passed.

As you say, however, it doesn't make a whole lot of sense to code a
constant as the argument in the call; it would seem more logical for
that to be a variable read in or somehow else set. But, that wasn't the
question asked.... :)

--
From: m_b_metcalf on
On May 7, 3:56 pm, ivan martin <i.mar...(a)invalid.com> wrote:
> Is it possible to transfer a string constant (I'm not sure if this is the exact terminology)
> to a subroutine ?
>
> What I'm trying to accomplish is something akin to this (fortran pseudo code);
> *******
>         call my_subroutine('string constant',number1)
>         end
>
>         subroutine my_subroutine(string,number1)
>         character(90) string
>         real number1
>
>         write(*,'(a)')string !why is this not working ?
>         return
>         end
> *******
>
> Why do I need this when I could simply load the "string constant" from the console, using READ
> statement ?
> I usually use two programs, one for generating an input file, and the other for processing that
> file. So, instead of reading the "string constant" from the user (with READ), I'm trying just to
> be able to enter the name of the test and loading and so on ... directly from the program.
> But when I try to run the above program, although it compiles relatively ok (one warning), it writes
> out garbage (some machine code which I cannot copypaste in here; but can be considered garbage).
>
> Please, any ideas. Where am I going wrong ?
> -- Ivan

You have a length mismatch for the character argument (which was
probably what the warning message that you chose ignore was telling
you). Try to use a * instead of 90 (known as assumed character
length).

HTH

Mike Metcalf
From: ivan martin on
On Fri, 7 May 2010 07:31:00 -0700 (PDT), m_b_metcalf <michaelmetcalf(a)compuserve.com> wrote:

>You have a length mismatch for the character argument (which was
>probably what the warning message that you chose ignore was telling
>you). Try to use a * instead of 90 (known as assumed character
>length).
>
>HTH
>
>Mike Metcalf


Hi Mike, thanks for answering (you too dpb !)

No, the first warning message was:
C:\fortran_snippets\string_con.f90(1) : Warning: In the call to MY_SUBROUTINE, actual argument #3
does not match the type and kind of the corresponding dummy argument.
call my_subroutine('string constant',number1)
---------------------------------------------^
(you might have some trouble viewing this on variable width font, the "arrow" is pointing to the
parenthesis just after "number1")

I'm not sure, but I don't think he was complaining about the assumed character length (btw, the
compiler which produced this message is Compaq's CVF66c).

I took dpb's advice, and changed in the subroutine
character(90) ... to character*(*), but the warning remains. The good side is it solved the problem
(it shows the string correctly now).

The reason I did it this way because I thought he was gonna pad the rest (to 90 characters) of the
string with blanks. But from your and dpb's answer, I suspect I'm doing something wrong in here (am
I ?). From your experience, what would be the correct way to handle this situation ?

-- Ivan
From: robin on
"dpb" <none(a)non.net> wrote in message news:hs178u$thi$1(a)news.eternal-september.org...
| ivan martin wrote:
| > Is it possible to transfer a string constant (I'm not sure if this is the exact terminology)
| > to a subroutine ?
|
| Of course...
|
| > What I'm trying to accomplish is something akin to this (fortran pseudo code);
| > *******
| > call my_subroutine('string constant',number1)
| > end
| >
| > subroutine my_subroutine(string,number1)
| > character(90) string
| > real number1
| >
| > write(*,'(a)')string !why is this not working ?
| > return
| > end
| > *******
| >
| ...
|
| Mismatch in length as written -- use assumed length in the subroutine
| instead.

There is a mismatch also in the second argument, which is INTEGER,
passed to a REAL.

| character*(*) :: string
|
| and the local copy will take the length of the actual string passed.
|
| As you say, however, it doesn't make a whole lot of sense to code a
| constant as the argument in the call; it would seem more logical for
| that to be a variable read in or somehow else set. But, that wasn't the
| question asked.... :)