# Array of structs

**URL:** <https://discourse.julialang.org/t/array-of-structs/3281>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 19, 2017, 11:56am UTC](https://discourse.julialang.org/t/array-of-structs/3281 "2017-04-19T11:56:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jilani](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jilani](https://discourse.julialang.org/u/jilani)\
**Post date:** [April 19, 2017, 11:56am UTC](https://discourse.julialang.org/t/array-of-structs/3281/1 "2017-04-19T11:56:49Z")

</div>

Hi All,  
I have some problems using Julia arrays with structs. I need some examples  
I tried to translate this Free Pascal code in Julia to compare their performance but I have had a lot of errors in Julia code:

```julia
const MaxPoints = 100;

type 
Point2D = record
  rank: integer;
  x, y: double;
end;  

ArrayOfPoints2D = array[1..MaxPoints] of Point2D;

CurveRec = record
  pts: ArrayOfPoints2D;
  AvarageHeight, surface, volume: double;
end;

// Now I can define some variables
var
  pts: ArrayOfPoints2D;
  C: CurveRec;
  tempR1, tempR2: double;
  st: string;
  i: integer;
// Now I can use pts and C variables this way
begin
  for i := 1 to MaxPoints do
  begin
    pts[i].rank := i;
    tempR1 := 2.71828*random(100);
    tempR2 := 3.14159*random(50);
    pts[i].x := tempR1;
    pts[i].y := tempR2;
    C.pts[i].x := pts[i].x;
    C.pts[i].y := pts[i].y;
	//...
  end;
end.
  
  
const MaxPoints = 1024
type Point2D
    x::Float64
    y::Float64
  end  

type ArrayOfPoints2D Array{Point2D}(MaxPoints) end

type CurveRec
  pts::ArrayOfPoints2D
  AvarageHeight::Float64
  surface::Float64
  volume::Float64
end

```

How to transalate the remaining Pascal code in Julia?  
Thank you.

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 19, 2017, 12:44pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/2 "2017-04-19T12:44:09Z")

</div>

What part of the code do you have problems with? Loops, assignments, creating random numbers?

Also, please consider wrapping your code into backticks, e.g.

````
```
your code here
```

````

---

<div class="post-metadata">

**Author:** ![Jilani\_KHALDI](https://avatars.discourse-cdn.com/v4/letter/j/ce73a5/32.png) [@Jilani\_KHALDI](https://discourse.julialang.org/u/Jilani_KHALDI)\
**Post date:** [April 19, 2017, 5:19pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/3 "2017-04-19T17:19:47Z")

</div>

From variables declarations (var…) to the end of Pascal code. In other words I asked the translation of Pascal code to Julia.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [April 19, 2017, 5:27pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/4 "2017-04-19T17:27:54Z")

</div>

Construct an array of structs with size `maxpoints` simply by saying

```
array_of_points_2d = Array{Point2D}(maxpoints)

```

---

<div class="post-metadata">

**Author:** ![jilani](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@jilani](https://discourse.julialang.org/u/jilani)\
**Post date:** [April 19, 2017, 6:04pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/5 "2017-04-19T18:04:59Z")

</div>

It is already done in the code I sent, the question is how to declare a variable of that type and use it as in Pascal code.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 19, 2017, 6:16pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/6 "2017-04-19T18:16:51Z")

</div>

What specific part is the problem?

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [April 19, 2017, 9:15pm UTC](https://discourse.julialang.org/t/array-of-structs/3281/7 "2017-04-19T21:15:19Z")

</div>

> [@jilani](#):
>
> the question is how to declare a variable of that type and use it as in Pascal code.

Julia is dynamically typed and doesn’t require preliminary variable declaration. If you want a variable `x` of type `Int` with initial value 42, you can just write:

```julia
x = 42

```

Note, that we didn’t declare type of `x`, because type is an attribute of a value, not variable (although you can explicitly ensure the type if local variable using syntax `local x::Int = 42`).

If you want to construct an array of points instead of an integer, you can do it exactly with the syntax proposed by @mbauman:

```julia
array_of_points_2d = Array{Point2D}(maxpoints) 

```

For further reference, I highly recommend reading the basics of the language, at least something like [Learn Julia in Y Minutes](https://learnxinyminutes.com/docs/julia/). Julia community is highly friendly, but as any community, it focuses on helping people to learn the language, not on doing some work for them. And the best resources for learning are tutorials and books.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [June 7, 2017, 6:13am UTC](https://discourse.julialang.org/t/array-of-structs/3281/8 "2017-06-07T06:13:14Z")

</div>

```julia
type xnode;
  centroid;
  splitvec;
  xparent;
  pscore;
  xdep;
  end;
  
function collectxnode(cents, xenc, xpar, pscores, xdep)
  n = size(cents,1);
  x = [];
  for(iter = 1:n)
    push!(x, xnode(cents[iter, :], xenc .== iter, xpar, pscores[iter], xdep));
    end;
  return(x);
  end;

```
