% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={cpp-pointers},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{cpp-pointers}
\author{}
\date{}

\begin{document}
\maketitle

\hypertarget{cpp-pointers}{%
\section{cpp-pointers}\label{cpp-pointers}}

\hypertarget{cpp-pointers-1}{%
\section{cpp Pointers}\label{cpp-pointers-1}}

\textbf{if p is a pointer}

\&p the memory address of p

*p the content that p points to

p the content of p (or the address that p points to)

\hypertarget{dynamic-memory-allocation-or-alias}{%
\subsection{Dynamic memory allocation or
alias}\label{dynamic-memory-allocation-or-alias}}

\textbf{Useful but dangerous\ldots{}}

.. code-block:: cpp

int \emph{p, }q; // create two pointers, named p and q

p = new int; //

*p = 100;

q = p;

*q = 200;

q = new int;

\emph{q = }p;

delete p; // give memory that p occupied back to operating system

p = NULL; // set p to memory address 0

delete q; // give memory that q occupied back to operating system

q = NULL; // set q to memory address 0

\hypertarget{dynamic-allocation-for-arrays}{%
\subsection{Dynamic Allocation for
arrays}\label{dynamic-allocation-for-arrays}}

.. code-block:: cpp

int size = 5; // create an integer int *A; // create an array of
pointers

A = new int{[} size {]}; // Ask for a certain size for your array

A{[}0{]} = 10; // Give values to Array A{[}1{]} = 20; A{[}2{]} = 30;
A{[}3{]} = 40; A{[}4{]} = 50;

// 8 32 32 36 40 cout \textless\textless{} \&A \textless\textless{} A
\textless\textless{} A + 0 \textless\textless{} A + 1
\textless\textless{} A + 2

// 10 28 30 cout \textless\textless{} A{[}0{]} \textless\textless{}
A{[}1{]} \textless\textless{} A{[}2{]}

// 10 10 28 30 cout \textless\textless{} \emph{A \textless\textless{} }A
+ 0 \textless\textless{} A + 1 \textless\textless{} * A + 2

delete {[}{]}A; A = NULL;

\end{document}
