Skip to main content

Exercises 2.14 Programming Exercise


Write a program in C that creates a display similar to Figure 2.13.2.


The C statement char *stringPtr = "Hello world.\n"; will give you some characters to display and the pointer variable, stringPtr, to access them. The printf formatting codes are %p for the pointer variable and 0x%02x to display the byte at each address in hexadecimal. If pointer variables are new to you, use *stringPtr to access the memory pointed to by the variable; * is called the dereferencing operator.

/* stringInHex.c
 * displays each character in "Hello world" in hex
 * and the address where it is stored.
 * 2017-09-29: Bob Plantz

#include <stdio.h>

int main(void)
  char *stringPtr = "Hello world.\n";

  while (*stringPtr != '\0')
    printf("%p:  ", stringPtr);
    printf("0x%02x\n", *stringPtr);
  printf("%p:  ", stringPtr);
  printf("0x%02x\n", *stringPtr);

  return 0;