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

  • Home
  • SEARCH
  • 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 7991519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:18:48+00:00 2026-06-04T13:18:48+00:00

I would like to call a C api function from Fortran. The C function

  • 0

I would like to call a C api function from Fortran. The C function accepts an array of bytes:

void image(const void *bitmap, int w, int h);

where three successive bytes in *bitmap represent an RGB colour tripple and are interpreted as unsigned char in C. I want to initialize the bitmap in Fortran and take care of drawing inside C. Current definition in Fortran uses

integer*1 rgbImage(6,2)

to initialize an image of 2×2 for example, but compiler won’t accept assignment

rgbImage(1,1) = 255

to get red colour. I’ve seen hints of using BYTE, UNSIGNED*1, LOGICAL*1 for unsigned single bytes, but gfortran (MacPort’s gcc 4.4 or 4.6 under Mac OS X) isn’t really happy with either of them. I could probably get away by cheating and assigning value -1 instead of 255, but that is very uncomfortable to use. The compiler flag -fno-range-check helped compile the code, but might not be available in other Fortran compilers and I consider it an ugly solution (I would still want to catch other warning). The values 'FF'X or '11111111'B are also recognized as 32-bit integers.

It is highly desirable for the code to be portable across different Fortran compilers.

  • 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-06-04T13:18:50+00:00Added an answer on June 4, 2026 at 1:18 pm

    My suggestion would be to use CHARACTER variables, and use ACHAR to set values (and ICHAR to convert back to integers as necessary). That should get you what you want and be completely portable. eg,

    character, dimension(6,2) :: rgbImage
    
    rgbImage(1,1) = achar(255)
    

    Updated to add: if you’re going to use the Fortran 2003 iso_c_binding stuff to interface to the C routines (highly recommended!) then you might as well make that rgbImage array characters of kind c_char, eg

    character(kind=c_char), dimension(6,2) :: rgbImage
    integer(kind=c_int) :: w, h
    
    ...
    rgbImage(1,1) = achar(255)
    ...
    
    call image(rgbImage, w, h)
    

    where you’ve defined the interface for the routine

     interface
        subroutine image(img, w, h) bind(C)
           use, intrinsic :: iso_c_binding
           implicit none
           integer(kind=c_int), intent(in), value :: w, h
           character(kind=c_char) :: img(:,:)
        end subroutine image
     end interface
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the c-api, I would like to call a python function by name. I
I am getting a list from an API call. I would like to be
I would like to call a Python script from within a Bash while loop.
I have a vector of pointers. I would like to call a function for
I've got a pickerview. I would like to call a specific function each time
I have an unmanaged COM object, and I would like to call it from
How would I call an ASP.NET Web API directly from code-behind? Or should I
I would like to call the event handler just one time, and then detach
I have a data set that that I would like to call in a
For logging purposes, I would like to call the .ToString() method of every object

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.