home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / sgi / 13138 < prev    next >
Encoding:
Text File  |  1992-09-01  |  4.1 KB  |  114 lines

  1. Path: sparky!uunet!olivea!sgigate!odin!dinkum!calvin
  2. From: calvin@dinkum.wpd.sgi.com (Calvin H. Vu)
  3. Newsgroups: comp.sys.sgi
  4. Subject: Re: C/FORTRAN Communication Problem
  5. Message-ID: <1992Sep1.153042.4283@odin.corp.sgi.com>
  6. Date: 1 Sep 92 15:30:42 GMT
  7. References: <31751@adm.brl.mil>
  8. Sender: news@odin.corp.sgi.com (Net News)
  9. Organization: Silicon Graphics, Inc.  Mountain View, CA
  10. Lines: 101
  11. Nntp-Posting-Host: dinkum.wpd.sgi.com
  12.  
  13. In <31751@adm.brl.mil> nobody@kodak.com writes:
  14.  
  15. | >From:    NAME: Arup K. Ghose                 
  16. |     FUNC: Biophys. & Compu. Chem.         
  17. |     TEL: (518) 445-7059                   <GHOSEAK@A1@DSRGVJ>
  18. | To:    "info-iris@BRL.MIL"@kodakr@mrgate@wpc
  19. |           I have some problem in communication between a Fortran (calling)
  20. |           routine and a C routine. Only speciality here is that I want to
  21. |           keep a character variable as char* in the C routine parameter. The
  22. |           programs are:
  23. |           
  24. |           prog1.f
  25. |           
  26. |                 integer*4 wing,item,ncall
  27. |                 character*13 totline
  28. |                 ncall = 5
  29. |                 wing = 2
  30. |                 totline(1:13)='Arup K. Ghose'
  31. |                 write(*,'('' Main_b:: ncall&wing='',i3,2x,i3)') ncall,wing
  32. |                 write(*,'('' Main_b:: totline ='',a)') totline
  33. |                 item = dispmenu(wing,ncall,totline)
  34. |                 write(*,'('' Main_a:: item&ncall&wing='',3(i3,2x))') 
  35. |                & item,ncall,wing
  36. |                 write(*,'('' Main_a:: totline ='',a)') totline
  37. |                 stop
  38. |                 end
  39. |           
  40. |           
  41. |           prog2.c
  42. |           
  43. |           /* CENTRY */
  44. |           int dispmenu_(wing,ncall,totline) 
  45. |           long int wing;
  46. |           long int ncall;
  47. |           char *totline;     
  48. |           {
  49. |                long int datum;
  50. |                printf("Dispmenu:: wing= %d \n", wing);
  51. |                printf("Dispmenu:: ncall= %d \n",ncall);
  52. |                printf("Dispmenu:: Totline= %s \n",totline);
  53. |                datum = 20;  
  54. |                return datum;
  55. |           } /* ENDCENTRY */
  56. |           
  57.     There are a few bugs in the C routine:
  58.  
  59.     1) the argument WING and NCALL should be declared as 'long int *'
  60.     since Fortran passes the arguments by reference.
  61.     2) There should be an extra argument at the end which contains the
  62.     length of the character string.
  63.     3) Fortran does not guarantee character variables to be 
  64.     null-terminated so the C routine must make sure that the string is
  65.     null-terminated when printing it out otherwise it could exceed the
  66.     accessible address space and cause a coredump.
  67.  
  68.     A better form of the C function is:
  69.  
  70.           int dispmenu_(wing,ncall,totline, linelen)
  71.           long int *wing;
  72.           long int *ncall;
  73.           char *totline;
  74.           long int linelen;
  75.           {
  76.                long int datum;
  77.                char *format = "Dispmenu:: Totline= %         ";
  78.                printf("Dispmenu:: wing= %d \n", wing);
  79.                printf("Dispmenu:: ncall= %d \n",ncall);
  80.         /* set up a variable format depending on the length of
  81.         the character string so the following printf will only
  82.         print upto that length
  83.          */
  84.                sprintf( format+21, ".%ds \n", linelen);
  85.                printf(format,totline);
  86.                datum = 20;
  87.                return datum;
  88.           } /* ENDCENTRY */
  89.  
  90. |           
  91. |           This program seems to me very similar to the example discussed
  92. |           under 'Uaing mkf2c and extcentry' on page 3-16 in Fortran 77
  93. |           Programmer's Guide. Unfortunately the comparable Makefile creates
  94. |           the executable code without any error message, but it dumps core
  95. |           due to Segmentation fault. 
  96.     
  97.     You should also look at section 3.1.2 which discusses argument
  98. passing between C and Fortran.
  99.  
  100. |           Any help will be appreciated. Thanks,
  101. |           
  102. |           Arup Ghose
  103. |           aghose@kodak.com
  104.  
  105. Hope that helps,
  106.  
  107. - calvin
  108. --
  109. -----------------------------------------------------------------------------
  110. Calvin H. Vu               | "We are each of us angels with only one
  111. Silicon Graphics Computer Systems  | wing.  And we can only fly embracing
  112. calvin@sgi.com   (415) 962-3679       | each other."
  113.