# How to reference the address of a two dimensional array's element correctly?

**URL:** <https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263>\
**Category:** Help & Support\
**Created:** [January 14, 2023, 7:57pm UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263 "2023-01-14T19:57:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 14, 2023, 7:57pm UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/1 "2023-01-14T19:57:47Z")

</div>

I have already asked that question on [StackOverflow](https://stackoverflow.com/questions/75117254/taking-the-address-of-a-two-dimensional-arrays-element/75118460#75118460) forum, but it seems that most Crystal programmers are here. I am just trying to understand how to work with pointers in Crystal, I know they are “not safe”, and I don’t need that explanation since I have a C programming background.

So the example is gonna be the same, I need to point the element `[x][y]` of the array (which is of type `Room`). I have modified my code according to the information provided by Johannes Müller. Now my method for getting the address of an element looks like:

```auto
def map_ptr(x : Int32, y : Int32)
  p = @map.to_unsafe[x].to_unsafe + y
  pointerof(p)
end

```

The type it gives me is a `Pointer(Pointer(Room)`, which seems odd. The fact that I point to a local variable seems also wrong, but if I do not pass it as a variable I get the error:

```auto
Error: can't take address of @map.to_unsafe[x].to_unsafe + y

```

However, I can not dereference the above pointer correctly. When I type:

```auto
current_room = map_ptr(0, 0)
current_room.value.value.my_method

```

then error says:

```auto
Error: undefined method 'value' for Room (compile-time type is (Pointer(Room) | Room))

```

but saying `current_room.value.my_method` is also wrong as it gives me:

```auto
Error: undefined method 'my_method' for Pointer(Room)

```

This seems like a never ending loop, what did I wrong?

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 15, 2023, 1:34am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/2 "2023-01-15T01:34:10Z")

</div>

I have find out that the redundant pointer type was caused by taking a `pointerof` of `@map.to_unsafe[x].to_unsafe + y` which is a pointer already. In stead that function appears to be more like:

```auto
def map_ptr(x : Int32, y : Int32)
  @map.to_unsafe[x].to_unsafe + y
end

```

However, that gives me a memory access error while trying to access it’s value like:

```auto
current_room = map_ptr(0, 0)
current_room.value.my_method

```

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 15, 2023, 8:01am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/3 "2023-01-15T08:01:21Z")

</div>

Is `map` empty? Are any of the arrays inside it empty? Dereferencing the first element of an empty array would definitely cause this.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 15, 2023, 8:46am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/4 "2023-01-15T08:46:10Z")

</div>

That almost seems like it… but I have no idea how it is possible since I am filling the `@map` in the initialization, before actually calling the function that is accessing it.

I have now initialized the map with `nil` before actually filling it, and I see that the first element is of type `Nil` which gives me a compile time error:

```auto
Error: undefined method 'my_method' for Nil (compile-time type is (Room | Nil))

```

Am I still doing something wrong with the array initialization? Here is the initialization code:

```auto
@map = Array(Array(Room?)).new(SIZE) { Array(Room?).new(SIZE, nil) }
    
(0...SIZE).each do |x|
  (0...SIZE).each do |y|
    room_type = ROOMS.sample
    @map[x][y] = Room.new(room_type)
  end
end

```

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 15, 2023, 1:07pm UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/5 "2023-01-15T13:07:29Z")

</div>

I guess whatever is wrong with your program lies outside the limited code you have shared.  
Can you give a bigger picture example?

Also, what are you really trying to do? As I already mentioned on SO, taking a pointer of an array item is very unsafe.  
If you just take a pointer to immediately dereference it and call a method, what do you need the pointer for? You can just access the array element directly.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [January 15, 2023, 6:46pm UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/6 "2023-01-15T18:46:17Z")

</div>

> [@straight-shoota](#):
>
> what are you really trying to do? … taking a pointer of an array item is very unsafe.

I’ve also got this question, but I figured the OP has their reasons. They mentioned they recognize it’s not safe.

However, even if we ignoring the safety component, avoiding direct pointer dereferencing will probably indicate what’s actually going wrong because it involves operating on top of meaningful abstractions.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 16, 2023, 2:31am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/7 "2023-01-16T02:31:12Z")

</div>

Ya, in that case it would not make sens. The above code is merely a simplified example. I see I have to provide more context, as this just seems to confuse people no less then it is confusing me. So I cut all that pointer non-sens as it seems to be completely irrelevant and is just distracting from the actual problem.

The minimal version of the `Room` class, required for this context, may be like this:

```auto
require './meta'

class Room
  include RubyClass                                                
  attr_reader :type, :x, :y
  
  def initialize(type = "", x = 0, y = 0)
    @type = type
    @x = x
    @y = y
  end
end

```

The `RubyClass` module simply contains a macro to define the Ruby like `attr_reader`.

```auto
module RubyClass
  macro attr_reader(*args)
    {% for arg in args %}
      def {{arg.id}}
        @{{arg.id}}
      end 
    {% end %}
  end
end

```

Now after the map initialization, let’s say I want to print each room object in a loop:

```auto
(0...SIZE).each do |x|
  (0...SIZE).each do |y|
    # Returns an object of type Room
    puts @map[x][y]
  end
end

```

This works and prints the correct type and object id:

```auto
#<Room:0x7fe59e745d60>
#<Room:0x7fe59e745d40>
#<Room:0x7fe59e745d20>
#<Room:0x7fe59e745d00>
#<Room:0x7fe59e745ce0>
#<Room:0x7fe59e745cc0>
#<Room:0x7fe59e745ca0>
#<Room:0x7fe59e745c80>
#<Room:0x7fe59e745c60>
...

```

However, when I try to print an instance of the `Room`:

```auto
(0...SIZE).each do |x|
  (0...SIZE).each do |y|
    # Compile time error !!!
    puts @map[x][y].y
  end
end

```

I get a compile time error:

```auto
Error: undefined method 'y' for Nil (compile-time type is (Room | Nil))

```

Can somebody explain what is going on with that type variation? Here is a [minimal reproducible example](https://paste.debian.net/1267347/).

EDIT:  
I use Crystal 1.6.2 compiled at x86\_64-pc-linux-gnu with LLVM 14.0.6

---

<div class="post-metadata">

**Author:** ![mdwagner](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mdwagner/32/1077_2.png) [@mdwagner](https://forum.crystal-lang.org/u/mdwagner)\
**Post date:** [January 16, 2023, 3:11am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/8 "2023-01-16T03:11:30Z")

</div>

You could do this:

```crystal
puts @map[x][y].as(Room).y

```

But would probably prefer something like this instead:

```crystal
puts @map[x][y].try(&.y)

```

Also, why not just use [`Object#getter`](https://crystal-lang.org/api/1.7.0/Object.html#getter%28%2Anames%2C%26block%29-macro)? It’s pretty much equivalent to `attr_reader`.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 16, 2023, 6:00am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/9 "2023-01-16T06:00:48Z")

</div>

Here is my problem; one of my condition was looking like:

```auto
(accept = player.move(0, -1, @map)) &&
            (current_room = current_room.value.north)

```

So first `player` will check if he can move to a given room and change it’s position. The `move` function will return `true` or `false`, depending if it can move to a given position or not.

In this case compiler have forced at me to use `current_room.try(&.value).try(&.north))`, even when there is no possibility for the `current_room.value.north` to be `nil`.

That seems like a bug to me, or a very paranoid type system at best.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 16, 2023, 6:16am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/10 "2023-01-16T06:16:22Z")

</div>

> [@mdwagner](#):
>
> But would probably prefer something like this instead:
> 
> ```auto
> puts @map[x][y].try(&.y)
> 
> ```

That is exactly what I was trying to avoid, since I thought that requires me to support `Pointer(Room | Nil) | Nil` kind of type. However that seems like my pointer is actually returning the correct type (`Pointer(Room | Nil)`) since it does never return `nil`. Nevertheless, thank you, that helped me to debug my code.

> [@mdwagner](#):
>
> Also, why not just use [`Object#getter`](https://crystal-lang.org/api/1.7.0/Object.html#getter%28%2Anames%2C%26block%29-macro)? It’s pretty much equivalent to `attr_reader`.

Yes, I know that macro. I guess I just like to have something familiar around and I am an unconditional Ruby lover 😊

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 16, 2023, 10:46am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/11 "2023-01-16T10:46:23Z")

</div>

Thanks for sharing the reproducible example, that explains it:

You declare the type of `@map` as `Array(Array(Room?))`.

```crystal
@map = Array(Array(Room?)).new(SIZE) { Array(Room?).new(SIZE, nil) }

```

That type allows the inner array to contain `nil` values which you then have to consider whenever you fetch an element. I figure that type is simply incorrect. You don’t want to have `Nil` values there, only `Room`.

This is an example for how you can improve this and also shorten the entire array construction code:

```crystal
    @map = Array(Array(Room)).new(SIZE) { |x|
      Array(Room).new(SIZE) { |y|
        room_type = ROOMS.sample
        Room.new(room_type, x, y)
      }
    }

```

[https://carc.in/#/r/eegz](https://carc.in/#/r/eegz)

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [January 16, 2023, 10:48am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/12 "2023-01-16T10:48:52Z")

</div>

> [@siery](#):
>
> Yes, I know that macro. I guess I just like to have something familiar around and I am an unconditional Ruby lover 😊

I think most people here love Ruby. That’s how Crystal came to be in the first place.  
But not everything in Ruby is great, so Crystal tries to make some things better, even if your used to the Ruby way. And when in Rome, do as the Romans do. It’ll be better for you and for anyone who’s trying to read your code when it uses Crystal idioms instead of custom Ruby-like idioms.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 16, 2023, 11:25am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/13 "2023-01-16T11:25:53Z")

</div>

Thank you. That makes sens now. So, completely not related to what I was assuming. When I apply that to my code there is no need to check for each element being `nil`.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 16, 2023, 11:33am UTC](https://forum.crystal-lang.org/t/how-to-reference-the-address-of-a-two-dimensional-arrays-element-correctly/5263/14 "2023-01-16T11:33:18Z")

</div>

Yes, I will consider changing that when I start sharing my code. Right now I just pick up Crystal over the weekend and wanna check how it stands against C.

It took me a few days to get how some things works, but I have not learned C over a day either, so that’s fair. I just assumed it’s gonna be much more like Ruby, and that was a bit misleading.
