Skip to main content
Engineering LibreTexts

8.3: Pointer Dereferencing

  • Page ID
    35837
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Suppose you have the following code fragment:

    char *pc, c, b;
    
    c = 1;
    pc = &c;
    

    We have declared two variables, a char and a pointer to a char. We then set the contents of the char to 1, and set the contents of the pointer to the address of the char. We don’t really need to know what this address is, but for the sake of argument, let’s say that c is located at memory address 2000 while pc is located at memory address 3000. If we were to search the computer’s memory, at address 2000 we would find the number 1. At address 3000, we would find the number 2000, that is, the address of the char variable. In a typical system, this value could span 32 bits or 4 bytes. In other words, the memory used by pc is addresses 3000, 3001, 3002, and 3003. Conversely, in a 16 bit system, pc would only span 3000 and 3001 (half as many bytes, but far fewer possible addresses).

    As the contents of (i.e., value of) pc tell us where a char resides, we can get to that location, and thus the value of the char variable c. To do this, we dereference the pointer with an asterisk. We could say:

    b = *pc;
    

    Read this as “b gets the value at the address given by pc”. That is, pc doesn’t give us the value, rather, it tells us where to go to get the value. It’s as if you went to your professor’s office and asked for your grade, and instead he hands you a piece of paper that reads “I will e-mail it to you”. The paper doesn’t indicate your grade, but it shows you where to find it. This might sound unduly complicated at first but it turns out to have myriad uses. By the way, in this example the value of b will be 1 because pc points to c, which was assigned the value 1 at the start. That is, b gets the value at the address pc points to, which is simply c.


    This page titled 8.3: Pointer Dereferencing is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.