Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 899799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:16:55+00:00 2026-05-15T15:16:55+00:00

Apologies for the rather simple question, I just can’t seem to find ANY good

  • 0

Apologies for the rather simple question, I just can’t seem to find ANY good fortran docs.

I’m trying to write a function that reads from a unit, trims the output and appends the null terminator, something like:

character(*) function readCString()
  character*512 str
  read(1, *) str
  readCString = TRIM(str)//char(0)
  return
end function readCString

However, I KNOW this doesn’t work yet it compiles. Segmentation faults have not been my friend recently. Without the “character(*)” before the function keyword it will not compile, and with any value in place of the star it also breaks, most likely because:

TRIM(str)//char(0)

is not the same length as the number I put in place of the star.
I’m very new to fortran but am trying to interface some fortran code with C (hence the null terminator).

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:16:57+00:00Added an answer on May 15, 2026 at 3:16 pm

    character(star) is not a good idea to use as a function return. character(star) is not a dynamic variable length string — it means obtain the length from the caller. So I suggest either using a fixed length string as the function return, or using character(*) as a subroutine argument — but in that case the caller has to reserve the memory in advance, which just pushes fixing the length to the caller. If you truly don’t know the maximum length and must have a dynamicvariable length string, there are way to do it. There is a variable length string module. Fortran 2003 has allocatable scalers, though this isn’t widely implemented. You could use an array of single characters — allocatable arrays have been around a long time.

    Here is an method that works as Fortran:

    module test_func_mod
    contains
    function readCString()
      use iso_c_binding
      character (kind=c_char, len=512)  :: readCString
      character (kind=c_char, len=511) :: str
      read(1, *) str
      readCString = TRIM(str) // C_NULL_CHAR
      return
    end function readCString
    
    end module test_func_mod
    
    program test
    
    use test_func_mod
    
    open (unit=1, file="temp.txt")
    write (*, *) readCString()
    
    end program test
    

    If you want to interface Fortran and C, I recommend using the new ISO C Binding. It provides a standard and therefore portable method of interfacing Fortran and C or any language that uses C passing conventions. It is easier to use and less likely to break than the hacks that had to be used in the past. While officially part of Fortran 2003, it is already supported by numerous compilers. Strings used to be particularly difficult, with some compilers having hidden length arguments, etc. The ISO C Binding makes all of this transparent to the programmer. I remember giving some example code for a similar question (Fortran, C, and strings) on stackoverflow, but I can’t find the question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My apologies if this is a stupid question, however I can't find any information
I know this is a rather simple question, but I can't figure it out
Apologies for so basic a question, but I can't find an answer anywhere. When
Apologies in advance for a rather non-specific question: I am looking for some guidance
Apologies in advance for a policy, rather than a programming question, but given the
Apologies I can't post any code at this time cos I'm away from my
Apologies if this is a very simple question; I don't use XSLT very much
This is a rather elementary C# question; my apologies, but I haven't been able
Apologies for the rather verbose and long-winded post, but this problem's been perplexing me
Apologies for the long code post but am wondering if someone can help with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.