Skip to content

Writing an auxiliary function that returns a null terminated array of C strings #278

@vmagnin

Description

@vmagnin

Some GTK functions expect a null terminated array of C strings. For example, in examples/bazaar.f90 we have this piece of code for gtk_about_dialog_set_authors():

	character(len=14, kind=c_char), dimension(2), parameter :: authors = &
										  & ["Vincent MAGNIN", "James TAPPIN  "]
	character(kind=c_char), dimension(:), allocatable :: string
	character(kind=c_char), pointer, dimension(:) :: credit
	type(c_ptr), dimension(:), allocatable :: c_ptr_array
	...
	! To add authors we need a pointer toward a null terminated array of strings.
	! This code comes from src/gtk-hl-dialog.f90:
	allocate(c_ptr_array(size(authors)+1))

	do i = 1, size(authors)
	  call f_c_string(authors(i), string)
	  allocate(credit(size(string)))
	  ! A Fortran pointer toward the Fortran string:
	  credit(:) = string(:)
	  ! Store the C address in the array:
	  c_ptr_array(i) = c_loc(credit(1))
	  nullify(credit)
	end do
	! The array must be null terminated:
	c_ptr_array(size(authors)+1) = c_null_ptr
	! https://docs.gtk.org/gtk3/method.AboutDialog.set_authors.html
	call gtk_about_dialog_set_authors(dialog, c_ptr_array)
	deallocate(c_ptr_array)

It would be interesting to write in src/gtk-sup.f90 an auxiliary function that returns such a null terminated array of C strings. It would be called by examples/bazaar.f90, examples/menubar.f90 and src/gtk-hl-dialog.f90.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions