Pointers - Advanced Concepts

Pointers to Pointers

Declaring a pointer to pointer

int x = 12;
int *ptr = &x;
int **ptr_to_ptr = & ptr;

All **ptr_to_ptr, *ptr, x evaluate to the same value 12.

Arrays of Pointers

The most common use: in manipulation of strings

/* Initializing an array of pointers to type char. */
#include <stdio.h>
void main()
{
  char *message[8] = { "Four", "score", "and", "seven",
     "years", "ago,", "our", "forefathers" };
  int count;
  for (count = 0; count < 8; count++)
    printf("%s ", message[count]);
}

Advanced Features

malloc() function

/* Demonstrates the use of malloc() to allocate storage */
#include <stdio.h>
#include <stdlib.h>
main()
{
  char count, *ptr, *p;
  /* The exit() library function terminates the program. */
  ptr = malloc(35 * sizeof(char));
  if (ptr == NULL)
  {
    puts("Memory allocation error.");
    exit(1);
  }
  p = ptr;
  for (count = 65; count < 91 ; count++)
    *p++ = count;
  *p = '\0';
  puts(ptr);
}

Advanced String Manipulation

/* Using the strlen() function. */
#include <stdio.h>
#include <string.h>
void main()
{
  size_t length;
  har buf[80];
  while (1)
  {
    puts("\nEnter a line of text; a blank line terminates.");
    gets(buf);
    length = strlen(buf);
    if (length != 0)
      printf("\nThat line is %u characters long.", length);
    else
      break;
  }
}

/* Demonstrates strcpy(). */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void main()
{ 
  char source[] = "The source string.";
  char dest1[80];
  char *dest2, *dest3;
  printf("\nsource: %s", source );
  /* Copy to dest1 is okay because dest1 points to */
  /* 80 bytes of allocated space. */
  strcpy(dest1, source);
  printf("\ndest1:  %s", dest1);
  /* To copy to dest2 you must allocate space. */
  dest2 = (char *)malloc(strlen(source) +1);
  strcpy(dest2, source);
  printf("\ndest2:  %s", dest2);
  /* Copying without allocating destination space */
  /* The following could cause serious problems. */
  /* strcpy(dest3, source); */
}

/* Using the strncpy() function. */
#include <stdio.h>
#include <string.h>
main()
{
  char dest[] = "..........................";
  char source[] = "abcdefghijklmnopqrstuvwxyz";
  size_t n;
  while (1)
  {
    puts("Enter the number of characters to copy (1-26)");
    scanf("%d", &n);
    if (n > 0 && n< 27)
      break;
  }
  printf("\nBefore strncpy destination = %s", dest);
  strncpy(dest, source, n);
  printf("\nAfter strncpy destination = %s", dest);
}

/* The strncat() function. */
#include <stdio.h>
#include <string.h>
void main()
{
  char str1[27];
  char str2[] = "abcdefghijklmnopqrstuvwxyz";
  int n;
  for (n=1; n< 27; n++)
  {
    strcpy(str1, "");
    strncat(str1, str2, n);
    puts(str1);
  }
}

/* The strcmp() function. */
#include <stdio.h>
 #include <string.h>
void main()
{
  char str1[80], str2[80];
  int x;
  while (1)
  {
    /* Input two strings. */
    printf("\n\nInput the first string, a blank to exit: ");
    gets(str1);
    if ( strlen(str1) == 0 )
       break;
    printf("\nInput the second string: ");
    gets(str2);
    /* Compare them and display the result. */
    x = strcmp(str1, str2);
    printf("\nstrcmp(%s,%s) returns %d", str1, str2, x);
  }
}

Structures

struct date
{
       int month;
       int day;
       int year;
};

/* Program to illustrate a structure */
#include < stdio.h>
main()
{
       struct date
      {
       int month;
       int day;
       int year;
      };
      struct date today;
      today.month = 11;
      today.day = 12;
      today.year = 1996;
      printf("Today's date is %i/%i/%i.\n", today.month, today.day, today.year % 100);
}

Arrays of Structures

/* Demonstrates using arrays of structures. */
#include <stdio.h>
main()
{
  /* Define a structure to hold entries. */
  struct entry {
    char fname[20];
    char lname[20];
    char phone[10];
  };
  /* Declare an array of structures. */
  struct entry list[4];
  int i;
  /* Loop to input data for four people. */
  for (i = 0; i < 4; i++)
  {
    printf("\nEnter first name: ");
    scanf("%s", list[i].fname);
    printf("Enter last name: ");
    scanf("%s", list[i].lname);
    printf("Enter phone in 123-4567 format: ");
    scanf("%s", list[i].phone);
  }
  /* Print two blank lines. */
  printf("\n\n");
  /* Loop to display data. */
  for (i = 0; i < 4; i++)
  {
    printf("Name: %s %s", list[i].fname, list[i].lname);
    printf("\t\tPhone: %s\n", list[i].phone);
  }
}

Manipulation of data:
list[1] = list[5];
strcpy(list[1].phone, list[5].phone);
list[5].phone[1] = list[2].phone[3];

Complex Structures

#include < stdio.h>
main()
{
  struct date
  {
     int month;
     int day;
     int year;
   };
   struct vacation
   {
     struct date start_date;
     struct date end_date;
   };
   struct vacation christmas_vacation;
}

Unions

union shared {
        char c;
        int i;
 };

Bitwise Operators

Bitwise Logical Operators

struct emp_data {
      unsigned dental  : 1;
      unsigned health  : 2;
      char fname[20];
      char lname[20];
      char ssn[10];
};
struct emp_data workers[1000];
workers[0].dental  = 1;
workers[0].health = 1;
strcpy(workers[0].fname, "John");

About this document ...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 -html_version 3.0 c7.tex.

The translation was initiated by Vijay & on Thu Apr 2 10:48:51 EST 1998


Vijay &
Thu Apr 2 10:48:51 EST 1998