Re: initializing an array of user-defined data types



Bart Vandewoestyne wrote:
I have a rank 2 array of a user-defined type as follows:

type(piece), dimension(:,:), allocatable, private :: board

After allocating it, I want to initialize it so that after
initialization, I can check whether board(i,j) contains a piece
or not.

I've tried things like

board = NULL

... assign some pieces to board positions and leave others empty ...

if (board(i,j) == NULL) then
... do something ...
end if


but that doesn't seem to work.

What is the proper way to do this?

What about this sort of thing:


type :: piece
integer :: i = -1
real :: x = -999.99
end type piece
...
type(piece), dimension(:,:), allocatable :: board
...
allocate(board(10,10))
...
if ( board(i,j) == piece(-1,-999.99) ) then
board(i,j) = piece(i,real(j))
... do something else ...
end if


??


Maybe you could even do

type(piece), parameter :: mynull = piece(-1,-999.99)
....
if ( board(i,j) == mynull ) then
... do something ...
end if

but I've never tried that and have no idea if it's allowed syntax. Easy to test, though.


cheers,

paulv
.



Relevant Pages

  • initializing an array of user-defined data types
    ... I have a rank 2 array of a user-defined type as follows: ... After allocating it, I want to initialize it so that after ... assign some pieces to board positions and leave others empty ... ...
    (comp.lang.fortran)
  • Re: multiple definitions....
    ... > also i think with a GNU compiler) I get no errors or warnings. ... simply because i am allocating memory for it ... When a global is allocated by more than one translation unit, the linker (or ... When both tu initialize the global, ...
    (comp.lang.c)
  • Re: initializing an array of user-defined data types
    ... After allocating it, I want to initialize it so that after ... And, of course, the usage notes to the NULLintrinsic include the following warning -- ... If you use module DFWIN or DFWINTY, you will have a name conflict if you use the NULL intrinsic. ...
    (comp.lang.fortran)
  • Re: VirtualAlloc and new
    ... > would imagine this would be slower than just allocating the raw memory. ... allocate raw memory and manually initialize ... I'm Schobi at suespammers dot org ...
    (microsoft.public.vc.language)
  • Re: initializer for array of struct
    ... > be a way to initialize each without allocating off the heap. ... Well, if it weren't in an array, it still wouldn't be allocating on the ...
    (microsoft.public.dotnet.languages.csharp)