# How to Draw Vertical Arrays with Diagonal Arrows to Represent Memory in TikZ?

I am trying to find a way to use TikZ to create as much as possible a collection of lecture notes (1), (2) for an introductory level course in data structures for a public free uni of my country.

I have been successful so far in finding trees, arrays and so on, but I was not able to find an starter point for representing memory and elements inside of them (wanted) with some arrows to point out concepts (optional) with diagonal arrows and code alignment (wanted).

I found that in other questions TeX community was receptive for such requests, I hope this one wouldn't be a problem.

The image below is from a book named Structures Gently Through C who has the visualization I am trying to achieve with TikZ.

-

Here's a sample which combines most of the features that you desire:

\documentclass{article}
%% Packages and libraries needed for this MWE
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{arrows.meta}
%% Two commands for representing content.
\newcommand\aeDiagonalNumber{100}
\newcommand\memorySixtyFive{%%'
\begin{minipage}{2.75cm}
\centering
char a' in memory\par
(65 = ascii code)
\end{minipage}}

\begin{document}

\begin{tikzpicture}[x=1.25in,y=0.65cm]
%% Building the boxes representing memory locations
\foreach \x in {0,1,...,20}
{
\node[inner sep=1pt] (BL\x) at (0,-\x)   {};
\node[inner sep=1pt] (UR\x) at (1,-\x+1) {};
\node (MM\x) at ($(BL\x)!0.5!(UR\x)$) {};
\draw (BL\x) rectangle (UR\x);
\edef\memoryNumber{\number\numexpr276314+\x\relax}
\node[anchor=south east] at (BL\x.north west) {\memoryNumber};
}

%% Label columns:
\node (MEMLOC) at ($(MM0)+(0,2cm)$) { \parbox{2cm}{\centering Memory Location} };
\node (ADDRESS) at ($(MEMLOC)-(0.75,0)$) {\parbox{2cm}{\centering Address} };
\draw [-{Stealth[scale=1.5]}] (MEMLOC) -- (MM0|-UR0);
\draw [-{Stealth[scale=1.5]}] (ADDRESS) -- ($(MM0|-UR0)-(0.75,0)$);

%% Drawing arrows spanning memory locations
\foreach \x/\y in {3/0,
7/4,
11/8}
{
\node (M\x-\y) at ($(BL\x)!0.5!(UR\y)$) {\aeDiagonalNumber};
\xdef\aeDiagonalNumber{\number\numexpr\aeDiagonalNumber+100\relax}
\draw[-{Stealth[scale=1.5]}]  (M\x-\y) -- (BL\x) ;
\draw[-{Stealth[scale=1.5]}]  (M\x-\y) -- (UR\y) ;
}

%% Adding content to memory location "18"
\node at (MM18) {65};
\node (RHS18) at (MM18-|UR18) {};
%% Adding a side note to memory location "18"
\node[draw,anchor=west] (BOX18) at ($(RHS18)+(1cm,0)$) {\memorySixtyFive};
\draw [-{Stealth[scale=1.5]}] (RHS18.center) -- (BOX18);

%%
\node (1byte) at ($(UR10)+(0.5,0)$) { \parbox{1.5cm}{\centering One location = 1 byte}};
\draw [-{Stealth[scale=1.5]}] (1byte) -- ($(1byte|-UR5)-(0,0.5)$) -- ($(UR5)-(0,0.5)$);

\end{tikzpicture}

\end{document}


Hopefully this illustrates enough of the techniques you can use to design the presentation in the manner you want.

-
Thank you so much A. Ellett. Now I can finish the class in time for tomorrow! :-) –  Oeufcoque Penteano Dec 7 '13 at 2:46
A.Ellett would you happen to know why arrows.meta can't be located? Is that a new package or something like this? I actually used it without the arrow symbols. No matter how much I google, all I could find was that it was supposed to change the arrow head size. But apparently no one else seems to know about it :( " I can't find file tikzlibraryarrows.meta.code.tex'." –  Oeufcoque Penteano Dec 12 '13 at 6:23
You'll need the latest CVS version of TikZ to get the arrow libraries. Sorry I forgot to mention that. –  A.Ellett Dec 12 '13 at 18:13
Thank you! :) I managed to get it to work around with triangles and the sorts. I will look into this later! –  Oeufcoque Penteano Dec 13 '13 at 1:38