Skip to main content
Engineering LibreTexts

8.2: Using Addresses and Pointers

  • Page ID
    35836
  • If we declare a variable as so:

    char a;
    

    then referencing a will get us the value stored in a, as in the code b=a;. Using the address of operator, as in &a, will obtain the memory location of a, not a’s value or contents. This is the sort of thing we used with scanf() and strcpy(). It is possible to declare variables that are designed to hold these addresses. They are called pointers. To declare a pointer, you preface the variable with an asterisk like so:

    char *pc;
    

    The variable pc is not a char, it is a pointer to a char. That is, its contents are the address of a char variable. The content of any pointer is an address. This is a very important point. Consider the following code fragments based on the declarations above:

    pc = a; /* unhappy */
    pc = &a; /* just fine */
    

    The first line makes no sense as we are trying to assign apples to oranges, so to speak. The second line makes perfect sense as both pc and &a are the same sort of thing, namely the address of a variable that holds a char. What if we want pointers to other kinds of things? No problem, just follow the examples below:

    float *pf;        /* pointer to a float */
    long int *pl;     /* pointer to a long int */
    double *pd, *p2;  /* two pointers to doubles */
    short int *ps, i; /* ps is a pointer to a short int */
                      /* i is just a short int */
    

    As mentioned, all pointers contain addresses. Therefore, no matter what the pointer points to, all pointers are the same size (same number of bytes). In most modern systems, pointers are either 32 bits (4 bytes) or 64 bits (8 bytes) although some small controllers use 16 bit addressing. When in doubt, you can check your code with sizeof(). If all pointers are the same size, then why do we declare different types of pointers? There are two reasons. First, this helps with type checking. Functions that take pointers as arguments or that return pointers will be using certain forms of data. You wouldn’t want to accidentally send off a pointer to a float when the function expects the address of a short int for example. Second, by specifying the type of thing the pointer points to, we can rely on the compiler to generate proper pointer math (more on this in a moment).

    • Was this article helpful?