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

Passing character array to a subroutine using a pointer

men8th
Beginner
902 Views
I need to pass a character array to a subroutine using a pointer. If I pass a character variable (i.e. not an array) as below then everything works OK:
Code:
 program Test_Project
   character(10), target :: strings
   character(10), pointer :: pstrings
   strings = 'string 1'
   pstrings => strings
   call test_sub(pstrings)
 end program Test_Project

 subroutine test_sub(str_in)
   character(len=*), intent(in) :: str_in
   print*, str_in
 end subroutine test_sub
However, if I attempt to pass a character array as follows then I get warning messages from the compiler about passing a character value toa non-character dummy argument and the code crashes when run.
Code:
 program Test_Project
   character(10), dimension(10), target :: strings
   character(10), dimension(:), pointer :: pstrings
   strings = 'string 1'
   pstrings => strings
   call test_sub(pstrings)
 end program Test_Project

 subroutine test_sub(str_in)
   character(len=*), intent(in), dimension(:) :: str_in
   print*, str_in
 end subroutine test_sub
What's going on? Any help or solutions would be most greatfully recieved. Cheers, Tim
0 Kudos
3 Replies
Steven_L_Intel1
Employee
902 Views
0 Kudos
men8th
Beginner
902 Views
Steve, thanks very much for the help. Looks like you've grown pretty tired of answering that question as you've gone to the trouble of putting the answer on a web-page!
Cheers, Tim
0 Kudos
Steven_L_Intel1
Employee
902 Views
It's actually an article I wrote several years ago, but this question does come up a lot!
0 Kudos
Reply