Computer History - Herong's Notes
Dr. Herong Yang, Version 3.00

Sample C Program for Arrays and Pointers

This section provides a sample C program using arrays and pointers. strdel(str,n,l) is function that deletes l characters at position n from str.

One main feature of the C language is that arrays and pointers are closely related to each other. Here is sample code that shows you how arrays of characters, char[], can be passed as pointers of characters, char*, in function calls.

/* strdel.c
*  strdel() function and test program
*/
#include <stdio.h>
extern int strdel(char*, int, int);
main() {
   char line[] = "abcde6789012345ABCDE";
   char ptr[80];

   strcpy(ptr,line);
   printf("string       : \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,1,5);
   printf("strdel( 1, 5): \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,5,5);
   printf("strdel( 5, 5): \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,5,25);
   printf("strdel( 5,25): \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,5,0);
   printf("strdel( 5, 0): \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,20,1);
   printf("strdel(20, 1): \"%s\"\n",ptr);

   strcpy(ptr,line);
   strdel(ptr,25,1);
   printf("strdel(25, 1): \"%s\"\n",ptr);
}
/* strdel(str,n,l) function
*  Deleting l characters at position n from str
*     ptr: string pointer
*     n: >=1, 1 means character one
*     l: >=0, 0 means to the end
*/
int strdel(str,n,l)
  char *str;
  int n, l;
{
   char *p;
   char *b;
   int m = 0;

   p = str;
   while (*p++) m++;    /* get the length, not including \0 */
   if (m<n) return;

   p = str;
   while ((n--)>1) p++;   /* get to the right position */
   *p = '\0';             /* good for l=0 */

   if (l>0 && n+l<=m) {
      b = p;
      while ((l--)>0) p++; /* skip l characters */
      while (*b++ = *p++); /* copy the rest, including \0 */
   }
}

The output of the sample code is:

string       : "abcde6789012345ABCDE"
strdel( 1, 5): "6789012345ABCDE"
strdel( 5, 5): "abcd012345ABCDE"
strdel( 5,25): "abcd"
strdel( 5, 0): "abcd"
strdel(20, 1): "abcde6789012345ABCD"
strdel(25, 1): "abcde6789012345ABCDE"

Table of Contents

 About This Book

 1957 - FORTRAN Language Developed by IBM

 1970 - UNIX Operating System Developed by AT&T Bell Labs

 1971 - FTP Protocol Created by Abhay Bhushan

1972 - C Language Developed by Dennis Ritchie

 What Is C Language?

 TCC - Tiny C Compiler

 Compilation and Execution Process of C Programs

 GNU C Compiler

Sample C Program for Arrays and Pointers

 Sample C Program for Dynamic Memory Allocation - malloc()

 1976 - vi Text Editor Developed by Bill Joy

 1977 - Apple II Designed by Steve Jobs and Steve Wozniak

 1977 - The Bourne Shell Developed by Stephen Bourne

 1978 - The C Shell Developed by Bill Joy

 1978 - Bash (Bourne-Again Shell) Developed by Brian Fox

 1979 - The Tcsh (TENEX C Shell) Developed by Ken Greer

 1983 - "Sendmail" Mail Transfer Agent Developed by Eric Allman

 1984 - Macintosh Developed by Apple Inc.

 1984 - X Window System Developed a MIT Team

 1991 - WWW (World Wide Web) Developed by Tim Berners-Lee

 1991 - Gopher Protocol Created by a University of Minnesota Team

 1995 - Java Language Developed by Sun Microsystems

 1995 - PHP: Hypertext Preprocessor Created by Rasmus Lerdorf

 2002 - .NET Framework Developed by Microsoft

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2012
Sample C Program for Arrays and Pointers