smval80
smval80

Reputation: 21

How to use structures with pointers in Fortran like in C++?

I have been using C++ for several years. Then I decided to use Fortran for better math performance. In C++ I have the following structure which I use everywhere:

structure     BitMap{
char*     rgba;         // pointer to the color array
int       w, h;};       // dimension of my bitmap

In Fortran, on the other hand:

Program Bitmap_p
implicit none
type BitMap
character :: rgba(*) ! like a pointer to bitmap array (colors and alpha)
integer:: w  ! bitmap width
integer:: h  ! bitmap height
endtype BitMap
endprogram Bitmap_p

However, when compiling this, the compilers states:

  1. f90 (4): error #6530: The array spec for this component must be of explicit shape and each bound must be an initialization expression. [RGBA]

Upvotes: 2

Views: 698

Answers (1)

L. Scott Johnson
L. Scott Johnson

Reputation: 4412

You should be able to use TYPE and POINTER in fortran to do what you can do with struct and * in C.

Program Bitmap_p
implicit none
type BitMap
character, pointer :: rgba(:) ! like a pointer to bitmap array (colors and alpha)
integer:: w  ! bitmap width
integer:: h  ! bitmap height
endtype BitMap

type(BitMap) :: bmap

bmap%w = 10
bmap%h = 10
allocate( bmap%rgba(4*bmap%w*bmap%h) )

endprogram Bitmap_p

Upvotes: 1

Related Questions