Pointers and Arrays


#include <stdio.h>
#include <stdlib.h>

int main () {

   /* Stack variables */
   int powers[] = {256, 512, 1024};
   char greeting[] = "hi";
   int *hp_ptr; 
   char *st_ptr;

   /* Allocates 8 bytes on the heap. 
      Stores the address of the first element in hp_ptr */
   hp_ptr = (int *) malloc (2 * sizeof(int));

   /* Stores the address of the second element of greeting in st_ptr */
   st_ptr = greeting+1;

   /* puts 128 in the first element in the array on the heap */
   hp_ptr[0] = 128;

   printf ("powers is at %p\n", powers);
   printf ("*powers is %d\n\n", *powers);

   printf ("greeting is at %p\n", greeting);
   printf ("*greeting is '%c'\n\n", *greeting);
   
   printf ("st_ptr is at %p\n", &st_ptr);
   printf ("st_ptr holds %p\n", st_ptr);
   printf ("*st_ptr is '%c'\n\n", *st_ptr);

   printf ("hp_ptr is at %p\n", &hp_ptr);
   printf ("hp_ptr holds %p\n", hp_ptr);
   printf ("*hp_ptr is %d\n", *hp_ptr);

   free (hp_ptr);

   return EXIT_SUCCESS;
}