Returning values from C function?

Steve Kargl sgk@troutmask.apl.washington.edu
Tue Dec 26 23:21:00 GMT 2006


On Tue, Dec 26, 2006 at 03:05:37PM -0500, Jeremy C wrote:
> Greetings! I am able to make this function in C:
> 
> int hello_(int *times, char *to_who) {
>  for (int idx=0; idx<*times; idx++) {
>    printf("Hello, %s!\n", to_who);
>  }
> 
>  return 10;
> }

#include <stdio.h>  /* Add missing header files.  */
int
hello_ (int *times, char *to_who)
{
  int idx;  /*  Fix warning.  */

  for (idx = 0; idx < *xtimes; idx++)
    printf("Hello, %s!\n", to_who);
 
  return 10;
}

> 
> integer :: a
> 
> print *, hello(10, "Me")
> 
> 

program hello_me
  integer, external :: hello
  print *, hello(10, "Me")
end program hello_me.

> I get "Hello, Me!" printed on the screen 10 times as expected, but it
> prints what seems like a pointer address.

The above works for me.

> How can I return values from C? Basic values like Integer, Float and 
> Strings?

You need to declare the functions as EXTERNAL.

-- 
Steve



More information about the Fortran mailing list