Home » Recent acquisitions » Acorn ADFS disks » adfs_AcornUser_199508.adf » !Regulars » Regulars/C/c/Struct

Regulars/C/c/Struct

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Recent acquisitions » Acorn ADFS disks » adfs_AcornUser_199508.adf » !Regulars
Filename: Regulars/C/c/Struct
Read OK:
File size: 1541 bytes
Load address: 0000
Exec address: 0000
File contents
/* C for Yourself Acorn User August 1995
   Example of structures
   by Steve Mumford */

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

#define  MAXSIZE 5

/* Global variables */
int   array[MAXSIZE]; /* This array will hold the numbers */
char  *strings[MAXSIZE]; /* This array will hold pointers to strings */
char  buffer[255]; /* A temporary storage space used to hold inputted strings */

typedef struct {  /* This is a structure - the pseudotype DATA is given to */
   int   number;  /* the definition, so it can be declared easily. Each */
   char  *string; /* structure holds one number and one pointer to a string. */
} DATA;

DATA  array2[MAXSIZE]; /* This declares an array of structures */

/* Declare some functions, so that the compiler knows about them in advance */

void cleanup(void);
void print1(void);
void print2(void);
void swapstruct(DATA *, DATA *);
void swapdata(int *, int *, char *[], int, int);

int
main(void)
{

   int   x, y;
   size_t   length; /* This will be used to hold the length of a string */
   char  key;

   printf("  This is a demonstration of the use of structures.\n");
   printf("The program will prompt you for %d strings and numbers\n", MAXSIZE);
   printf("and store them in two separate arrays as well as an array of\n");
   printf("structures.\n\n");
   printf("  After storing the data, the program will sort it according to\n");
   printf("the numbers you entered. Take a look at the swap functions to\n");
   printf("see how complicated things can get if you stick to using\n");
   printf("separate arrays...\n\n");
   for (x = 0; x < MAXSIZE; x++) {

      /* This is the data entry technique I used last month - I read a string
         into a large buffer first, then calculate the amount of memory
         needed to store it elsewhere and allocate that using the malloc()
         function. You have to remember to free the memory afterwards though! */

      printf("Please enter string number %d:\n", x+1);
      gets(buffer);
      length = strlen(buffer);
      printf("That string was %d bytes long.\n", length);

      if ((strings[x] = malloc(length+1)) == NULL) {
         printf("Memory allocation failed.\n");
         cleanup();
      }
      if ((array2[x].string = malloc(length+1)) == NULL) {
         printf("Memory allocation failed for structure.\n");
         cleanup();
      }

      /* Once the memory has been allocated, the strings can be copied
         into their permanent homes */

      strcpy(strings[x], buffer);
      strcpy(array2[x].string, buffer);
      
      printf("Please enter a number to go with that string:\n");
      scanf("%d", &array[x]);
      array2[x].number = array[x];
      while (getchar() != '\n');
      printf("\n\n");
   }

   printf("Here's the data you entered:\n");
   print1();
   printf("Please press the Return key:\n");
   key = getchar();
   print2();
   printf("Please press Return to sort the data according to number:\n");
   key = getchar();

   for (x = 0; x < MAXSIZE; x++) {
      for (y = x; y < MAXSIZE; y++) {
         if (array[x] > array[y]) {
            swapdata(&array[x], &array[y], strings, x, y);
         }
      }
   }

   for (x = 0; x < MAXSIZE; x++) {
      for (y = x; y < MAXSIZE; y++) {
         if (array2[x].number > array2[y].number) {
            swapstruct(&array2[x], &array2[y]);
         }
      }
   }

   printf("Data after sorting:\n");
   print1();
   printf("Please press the Return key:\n");
   key = getchar();
   print2();
   printf("Please press the Return key to leave the program:\n");
   key = getchar();

   cleanup();
}

void
cleanup(void)
{
   int   x;

   printf("Freeing memory.\n");
   for (x = 0; x < MAXSIZE; x++) {
      free(strings[x]);
      free(array2[x].string);     
   }
}

void
print1(void)
{
   int   x;

   printf("Data held in the two separate arrays:\n");
   printf("-------------------------------------\n\n");
   for (x = 0; x < MAXSIZE; x++) {
      printf("String %d: %s\n", x+1, strings[x]);
      printf("Number %d: %d\n\n", x+1, array[x]);
   }
}

void
print2(void)
{
   int   x;

   printf("Data held in the array of structures:\n");
   printf("-------------------------------------\n\n");
   for (x = 0; x < MAXSIZE; x++) {
      printf("String %d: %s\n", x+1, array2[x].string);
      printf("Number %d: %d\n\n", x+1, array2[x].number);
   }
}

void
swapstruct(DATA *point1, DATA *point2)
{
   DATA  temp;

   /* This function takes pointers to two structures and swaps their contents.
      Notice how they can be treated as single variables - this makes
      manipulating them a lot easier! */

   temp = *point1;
   *point1 = *point2;
   *point2 = temp;
}

void
swapdata(int *x, int *y, char *temparr[], int var1, int var2)
{
   /* Notice the increase in complexity from the swapstruct() function -
      every additional variable in the data record would need to be dealt
      with by hand in this function, whereas the swapstruct() function
      would not have to be altered. */

   int temp;
   char *stringtemp;

   /* This fragment takes pointers to two integers and swaps them */

   temp = *x;
   *x = *y;
   *y = temp;

   /* This messy code takes a pointer to an array of pointers to strings,
      and swaps the strings accordingly. You have to be in the right
      frame of mind to understand pointers... */

   stringtemp = temparr[var1];
   temparr[var1] = temparr[var2];
   temparr[var2] = stringtemp;
}
00000000  2f 2a 20 43 20 66 6f 72  20 59 6f 75 72 73 65 6c  |/* C for Yoursel|
00000010  66 20 41 63 6f 72 6e 20  55 73 65 72 20 41 75 67  |f Acorn User Aug|
00000020  75 73 74 20 31 39 39 35  0a 20 20 20 45 78 61 6d  |ust 1995.   Exam|
00000030  70 6c 65 20 6f 66 20 73  74 72 75 63 74 75 72 65  |ple of structure|
00000040  73 0a 20 20 20 62 79 20  53 74 65 76 65 20 4d 75  |s.   by Steve Mu|
00000050  6d 66 6f 72 64 20 2a 2f  0a 0a 23 69 6e 63 6c 75  |mford */..#inclu|
00000060  64 65 20 3c 73 74 64 69  6f 2e 68 3e 0a 23 69 6e  |de <stdio.h>.#in|
00000070  63 6c 75 64 65 20 3c 73  74 64 6c 69 62 2e 68 3e  |clude <stdlib.h>|
00000080  0a 23 69 6e 63 6c 75 64  65 20 3c 73 74 72 69 6e  |.#include <strin|
00000090  67 2e 68 3e 0a 0a 23 64  65 66 69 6e 65 20 20 4d  |g.h>..#define  M|
000000a0  41 58 53 49 5a 45 20 35  0a 0a 2f 2a 20 47 6c 6f  |AXSIZE 5../* Glo|
000000b0  62 61 6c 20 76 61 72 69  61 62 6c 65 73 20 2a 2f  |bal variables */|
000000c0  0a 69 6e 74 20 20 20 61  72 72 61 79 5b 4d 41 58  |.int   array[MAX|
000000d0  53 49 5a 45 5d 3b 20 2f  2a 20 54 68 69 73 20 61  |SIZE]; /* This a|
000000e0  72 72 61 79 20 77 69 6c  6c 20 68 6f 6c 64 20 74  |rray will hold t|
000000f0  68 65 20 6e 75 6d 62 65  72 73 20 2a 2f 0a 63 68  |he numbers */.ch|
00000100  61 72 20 20 2a 73 74 72  69 6e 67 73 5b 4d 41 58  |ar  *strings[MAX|
00000110  53 49 5a 45 5d 3b 20 2f  2a 20 54 68 69 73 20 61  |SIZE]; /* This a|
00000120  72 72 61 79 20 77 69 6c  6c 20 68 6f 6c 64 20 70  |rray will hold p|
00000130  6f 69 6e 74 65 72 73 20  74 6f 20 73 74 72 69 6e  |ointers to strin|
00000140  67 73 20 2a 2f 0a 63 68  61 72 20 20 62 75 66 66  |gs */.char  buff|
00000150  65 72 5b 32 35 35 5d 3b  20 2f 2a 20 41 20 74 65  |er[255]; /* A te|
00000160  6d 70 6f 72 61 72 79 20  73 74 6f 72 61 67 65 20  |mporary storage |
00000170  73 70 61 63 65 20 75 73  65 64 20 74 6f 20 68 6f  |space used to ho|
00000180  6c 64 20 69 6e 70 75 74  74 65 64 20 73 74 72 69  |ld inputted stri|
00000190  6e 67 73 20 2a 2f 0a 0a  74 79 70 65 64 65 66 20  |ngs */..typedef |
000001a0  73 74 72 75 63 74 20 7b  20 20 2f 2a 20 54 68 69  |struct {  /* Thi|
000001b0  73 20 69 73 20 61 20 73  74 72 75 63 74 75 72 65  |s is a structure|
000001c0  20 2d 20 74 68 65 20 70  73 65 75 64 6f 74 79 70  | - the pseudotyp|
000001d0  65 20 44 41 54 41 20 69  73 20 67 69 76 65 6e 20  |e DATA is given |
000001e0  74 6f 20 2a 2f 0a 20 20  20 69 6e 74 20 20 20 6e  |to */.   int   n|
000001f0  75 6d 62 65 72 3b 20 20  2f 2a 20 74 68 65 20 64  |umber;  /* the d|
00000200  65 66 69 6e 69 74 69 6f  6e 2c 20 73 6f 20 69 74  |efinition, so it|
00000210  20 63 61 6e 20 62 65 20  64 65 63 6c 61 72 65 64  | can be declared|
00000220  20 65 61 73 69 6c 79 2e  20 45 61 63 68 20 2a 2f  | easily. Each */|
00000230  0a 20 20 20 63 68 61 72  20 20 2a 73 74 72 69 6e  |.   char  *strin|
00000240  67 3b 20 2f 2a 20 73 74  72 75 63 74 75 72 65 20  |g; /* structure |
00000250  68 6f 6c 64 73 20 6f 6e  65 20 6e 75 6d 62 65 72  |holds one number|
00000260  20 61 6e 64 20 6f 6e 65  20 70 6f 69 6e 74 65 72  | and one pointer|
00000270  20 74 6f 20 61 20 73 74  72 69 6e 67 2e 20 2a 2f  | to a string. */|
00000280  0a 7d 20 44 41 54 41 3b  0a 0a 44 41 54 41 20 20  |.} DATA;..DATA  |
00000290  61 72 72 61 79 32 5b 4d  41 58 53 49 5a 45 5d 3b  |array2[MAXSIZE];|
000002a0  20 2f 2a 20 54 68 69 73  20 64 65 63 6c 61 72 65  | /* This declare|
000002b0  73 20 61 6e 20 61 72 72  61 79 20 6f 66 20 73 74  |s an array of st|
000002c0  72 75 63 74 75 72 65 73  20 2a 2f 0a 0a 2f 2a 20  |ructures */../* |
000002d0  44 65 63 6c 61 72 65 20  73 6f 6d 65 20 66 75 6e  |Declare some fun|
000002e0  63 74 69 6f 6e 73 2c 20  73 6f 20 74 68 61 74 20  |ctions, so that |
000002f0  74 68 65 20 63 6f 6d 70  69 6c 65 72 20 6b 6e 6f  |the compiler kno|
00000300  77 73 20 61 62 6f 75 74  20 74 68 65 6d 20 69 6e  |ws about them in|
00000310  20 61 64 76 61 6e 63 65  20 2a 2f 0a 0a 76 6f 69  | advance */..voi|
00000320  64 20 63 6c 65 61 6e 75  70 28 76 6f 69 64 29 3b  |d cleanup(void);|
00000330  0a 76 6f 69 64 20 70 72  69 6e 74 31 28 76 6f 69  |.void print1(voi|
00000340  64 29 3b 0a 76 6f 69 64  20 70 72 69 6e 74 32 28  |d);.void print2(|
00000350  76 6f 69 64 29 3b 0a 76  6f 69 64 20 73 77 61 70  |void);.void swap|
00000360  73 74 72 75 63 74 28 44  41 54 41 20 2a 2c 20 44  |struct(DATA *, D|
00000370  41 54 41 20 2a 29 3b 0a  76 6f 69 64 20 73 77 61  |ATA *);.void swa|
00000380  70 64 61 74 61 28 69 6e  74 20 2a 2c 20 69 6e 74  |pdata(int *, int|
00000390  20 2a 2c 20 63 68 61 72  20 2a 5b 5d 2c 20 69 6e  | *, char *[], in|
000003a0  74 2c 20 69 6e 74 29 3b  0a 0a 69 6e 74 0a 6d 61  |t, int);..int.ma|
000003b0  69 6e 28 76 6f 69 64 29  0a 7b 0a 0a 20 20 20 69  |in(void).{..   i|
000003c0  6e 74 20 20 20 78 2c 20  79 3b 0a 20 20 20 73 69  |nt   x, y;.   si|
000003d0  7a 65 5f 74 20 20 20 6c  65 6e 67 74 68 3b 20 2f  |ze_t   length; /|
000003e0  2a 20 54 68 69 73 20 77  69 6c 6c 20 62 65 20 75  |* This will be u|
000003f0  73 65 64 20 74 6f 20 68  6f 6c 64 20 74 68 65 20  |sed to hold the |
00000400  6c 65 6e 67 74 68 20 6f  66 20 61 20 73 74 72 69  |length of a stri|
00000410  6e 67 20 2a 2f 0a 20 20  20 63 68 61 72 20 20 6b  |ng */.   char  k|
00000420  65 79 3b 0a 0a 20 20 20  70 72 69 6e 74 66 28 22  |ey;..   printf("|
00000430  20 20 54 68 69 73 20 69  73 20 61 20 64 65 6d 6f  |  This is a demo|
00000440  6e 73 74 72 61 74 69 6f  6e 20 6f 66 20 74 68 65  |nstration of the|
00000450  20 75 73 65 20 6f 66 20  73 74 72 75 63 74 75 72  | use of structur|
00000460  65 73 2e 5c 6e 22 29 3b  0a 20 20 20 70 72 69 6e  |es.\n");.   prin|
00000470  74 66 28 22 54 68 65 20  70 72 6f 67 72 61 6d 20  |tf("The program |
00000480  77 69 6c 6c 20 70 72 6f  6d 70 74 20 79 6f 75 20  |will prompt you |
00000490  66 6f 72 20 25 64 20 73  74 72 69 6e 67 73 20 61  |for %d strings a|
000004a0  6e 64 20 6e 75 6d 62 65  72 73 5c 6e 22 2c 20 4d  |nd numbers\n", M|
000004b0  41 58 53 49 5a 45 29 3b  0a 20 20 20 70 72 69 6e  |AXSIZE);.   prin|
000004c0  74 66 28 22 61 6e 64 20  73 74 6f 72 65 20 74 68  |tf("and store th|
000004d0  65 6d 20 69 6e 20 74 77  6f 20 73 65 70 61 72 61  |em in two separa|
000004e0  74 65 20 61 72 72 61 79  73 20 61 73 20 77 65 6c  |te arrays as wel|
000004f0  6c 20 61 73 20 61 6e 20  61 72 72 61 79 20 6f 66  |l as an array of|
00000500  5c 6e 22 29 3b 0a 20 20  20 70 72 69 6e 74 66 28  |\n");.   printf(|
00000510  22 73 74 72 75 63 74 75  72 65 73 2e 5c 6e 5c 6e  |"structures.\n\n|
00000520  22 29 3b 0a 20 20 20 70  72 69 6e 74 66 28 22 20  |");.   printf(" |
00000530  20 41 66 74 65 72 20 73  74 6f 72 69 6e 67 20 74  | After storing t|
00000540  68 65 20 64 61 74 61 2c  20 74 68 65 20 70 72 6f  |he data, the pro|
00000550  67 72 61 6d 20 77 69 6c  6c 20 73 6f 72 74 20 69  |gram will sort i|
00000560  74 20 61 63 63 6f 72 64  69 6e 67 20 74 6f 5c 6e  |t according to\n|
00000570  22 29 3b 0a 20 20 20 70  72 69 6e 74 66 28 22 74  |");.   printf("t|
00000580  68 65 20 6e 75 6d 62 65  72 73 20 79 6f 75 20 65  |he numbers you e|
00000590  6e 74 65 72 65 64 2e 20  54 61 6b 65 20 61 20 6c  |ntered. Take a l|
000005a0  6f 6f 6b 20 61 74 20 74  68 65 20 73 77 61 70 20  |ook at the swap |
000005b0  66 75 6e 63 74 69 6f 6e  73 20 74 6f 5c 6e 22 29  |functions to\n")|
000005c0  3b 0a 20 20 20 70 72 69  6e 74 66 28 22 73 65 65  |;.   printf("see|
000005d0  20 68 6f 77 20 63 6f 6d  70 6c 69 63 61 74 65 64  | how complicated|
000005e0  20 74 68 69 6e 67 73 20  63 61 6e 20 67 65 74 20  | things can get |
000005f0  69 66 20 79 6f 75 20 73  74 69 63 6b 20 74 6f 20  |if you stick to |
00000600  75 73 69 6e 67 5c 6e 22  29 3b 0a 20 20 20 70 72  |using\n");.   pr|
00000610  69 6e 74 66 28 22 73 65  70 61 72 61 74 65 20 61  |intf("separate a|
00000620  72 72 61 79 73 2e 2e 2e  5c 6e 5c 6e 22 29 3b 0a  |rrays...\n\n");.|
00000630  20 20 20 66 6f 72 20 28  78 20 3d 20 30 3b 20 78  |   for (x = 0; x|
00000640  20 3c 20 4d 41 58 53 49  5a 45 3b 20 78 2b 2b 29  | < MAXSIZE; x++)|
00000650  20 7b 0a 0a 20 20 20 20  20 20 2f 2a 20 54 68 69  | {..      /* Thi|
00000660  73 20 69 73 20 74 68 65  20 64 61 74 61 20 65 6e  |s is the data en|
00000670  74 72 79 20 74 65 63 68  6e 69 71 75 65 20 49 20  |try technique I |
00000680  75 73 65 64 20 6c 61 73  74 20 6d 6f 6e 74 68 20  |used last month |
00000690  2d 20 49 20 72 65 61 64  20 61 20 73 74 72 69 6e  |- I read a strin|
000006a0  67 0a 20 20 20 20 20 20  20 20 20 69 6e 74 6f 20  |g.         into |
000006b0  61 20 6c 61 72 67 65 20  62 75 66 66 65 72 20 66  |a large buffer f|
000006c0  69 72 73 74 2c 20 74 68  65 6e 20 63 61 6c 63 75  |irst, then calcu|
000006d0  6c 61 74 65 20 74 68 65  20 61 6d 6f 75 6e 74 20  |late the amount |
000006e0  6f 66 20 6d 65 6d 6f 72  79 0a 20 20 20 20 20 20  |of memory.      |
000006f0  20 20 20 6e 65 65 64 65  64 20 74 6f 20 73 74 6f  |   needed to sto|
00000700  72 65 20 69 74 20 65 6c  73 65 77 68 65 72 65 20  |re it elsewhere |
00000710  61 6e 64 20 61 6c 6c 6f  63 61 74 65 20 74 68 61  |and allocate tha|
00000720  74 20 75 73 69 6e 67 20  74 68 65 20 6d 61 6c 6c  |t using the mall|
00000730  6f 63 28 29 0a 20 20 20  20 20 20 20 20 20 66 75  |oc().         fu|
00000740  6e 63 74 69 6f 6e 2e 20  59 6f 75 20 68 61 76 65  |nction. You have|
00000750  20 74 6f 20 72 65 6d 65  6d 62 65 72 20 74 6f 20  | to remember to |
00000760  66 72 65 65 20 74 68 65  20 6d 65 6d 6f 72 79 20  |free the memory |
00000770  61 66 74 65 72 77 61 72  64 73 20 74 68 6f 75 67  |afterwards thoug|
00000780  68 21 20 2a 2f 0a 0a 20  20 20 20 20 20 70 72 69  |h! */..      pri|
00000790  6e 74 66 28 22 50 6c 65  61 73 65 20 65 6e 74 65  |ntf("Please ente|
000007a0  72 20 73 74 72 69 6e 67  20 6e 75 6d 62 65 72 20  |r string number |
000007b0  25 64 3a 5c 6e 22 2c 20  78 2b 31 29 3b 0a 20 20  |%d:\n", x+1);.  |
000007c0  20 20 20 20 67 65 74 73  28 62 75 66 66 65 72 29  |    gets(buffer)|
000007d0  3b 0a 20 20 20 20 20 20  6c 65 6e 67 74 68 20 3d  |;.      length =|
000007e0  20 73 74 72 6c 65 6e 28  62 75 66 66 65 72 29 3b  | strlen(buffer);|
000007f0  0a 20 20 20 20 20 20 70  72 69 6e 74 66 28 22 54  |.      printf("T|
00000800  68 61 74 20 73 74 72 69  6e 67 20 77 61 73 20 25  |hat string was %|
00000810  64 20 62 79 74 65 73 20  6c 6f 6e 67 2e 5c 6e 22  |d bytes long.\n"|
00000820  2c 20 6c 65 6e 67 74 68  29 3b 0a 0a 20 20 20 20  |, length);..    |
00000830  20 20 69 66 20 28 28 73  74 72 69 6e 67 73 5b 78  |  if ((strings[x|
00000840  5d 20 3d 20 6d 61 6c 6c  6f 63 28 6c 65 6e 67 74  |] = malloc(lengt|
00000850  68 2b 31 29 29 20 3d 3d  20 4e 55 4c 4c 29 20 7b  |h+1)) == NULL) {|
00000860  0a 20 20 20 20 20 20 20  20 20 70 72 69 6e 74 66  |.         printf|
00000870  28 22 4d 65 6d 6f 72 79  20 61 6c 6c 6f 63 61 74  |("Memory allocat|
00000880  69 6f 6e 20 66 61 69 6c  65 64 2e 5c 6e 22 29 3b  |ion failed.\n");|
00000890  0a 20 20 20 20 20 20 20  20 20 63 6c 65 61 6e 75  |.         cleanu|
000008a0  70 28 29 3b 0a 20 20 20  20 20 20 7d 0a 20 20 20  |p();.      }.   |
000008b0  20 20 20 69 66 20 28 28  61 72 72 61 79 32 5b 78  |   if ((array2[x|
000008c0  5d 2e 73 74 72 69 6e 67  20 3d 20 6d 61 6c 6c 6f  |].string = mallo|
000008d0  63 28 6c 65 6e 67 74 68  2b 31 29 29 20 3d 3d 20  |c(length+1)) == |
000008e0  4e 55 4c 4c 29 20 7b 0a  20 20 20 20 20 20 20 20  |NULL) {.        |
000008f0  20 70 72 69 6e 74 66 28  22 4d 65 6d 6f 72 79 20  | printf("Memory |
00000900  61 6c 6c 6f 63 61 74 69  6f 6e 20 66 61 69 6c 65  |allocation faile|
00000910  64 20 66 6f 72 20 73 74  72 75 63 74 75 72 65 2e  |d for structure.|
00000920  5c 6e 22 29 3b 0a 20 20  20 20 20 20 20 20 20 63  |\n");.         c|
00000930  6c 65 61 6e 75 70 28 29  3b 0a 20 20 20 20 20 20  |leanup();.      |
00000940  7d 0a 0a 20 20 20 20 20  20 2f 2a 20 4f 6e 63 65  |}..      /* Once|
00000950  20 74 68 65 20 6d 65 6d  6f 72 79 20 68 61 73 20  | the memory has |
00000960  62 65 65 6e 20 61 6c 6c  6f 63 61 74 65 64 2c 20  |been allocated, |
00000970  74 68 65 20 73 74 72 69  6e 67 73 20 63 61 6e 20  |the strings can |
00000980  62 65 20 63 6f 70 69 65  64 0a 20 20 20 20 20 20  |be copied.      |
00000990  20 20 20 69 6e 74 6f 20  74 68 65 69 72 20 70 65  |   into their pe|
000009a0  72 6d 61 6e 65 6e 74 20  68 6f 6d 65 73 20 2a 2f  |rmanent homes */|
000009b0  0a 0a 20 20 20 20 20 20  73 74 72 63 70 79 28 73  |..      strcpy(s|
000009c0  74 72 69 6e 67 73 5b 78  5d 2c 20 62 75 66 66 65  |trings[x], buffe|
000009d0  72 29 3b 0a 20 20 20 20  20 20 73 74 72 63 70 79  |r);.      strcpy|
000009e0  28 61 72 72 61 79 32 5b  78 5d 2e 73 74 72 69 6e  |(array2[x].strin|
000009f0  67 2c 20 62 75 66 66 65  72 29 3b 0a 20 20 20 20  |g, buffer);.    |
00000a00  20 20 0a 20 20 20 20 20  20 70 72 69 6e 74 66 28  |  .      printf(|
00000a10  22 50 6c 65 61 73 65 20  65 6e 74 65 72 20 61 20  |"Please enter a |
00000a20  6e 75 6d 62 65 72 20 74  6f 20 67 6f 20 77 69 74  |number to go wit|
00000a30  68 20 74 68 61 74 20 73  74 72 69 6e 67 3a 5c 6e  |h that string:\n|
00000a40  22 29 3b 0a 20 20 20 20  20 20 73 63 61 6e 66 28  |");.      scanf(|
00000a50  22 25 64 22 2c 20 26 61  72 72 61 79 5b 78 5d 29  |"%d", &array[x])|
00000a60  3b 0a 20 20 20 20 20 20  61 72 72 61 79 32 5b 78  |;.      array2[x|
00000a70  5d 2e 6e 75 6d 62 65 72  20 3d 20 61 72 72 61 79  |].number = array|
00000a80  5b 78 5d 3b 0a 20 20 20  20 20 20 77 68 69 6c 65  |[x];.      while|
00000a90  20 28 67 65 74 63 68 61  72 28 29 20 21 3d 20 27  | (getchar() != '|
00000aa0  5c 6e 27 29 3b 0a 20 20  20 20 20 20 70 72 69 6e  |\n');.      prin|
00000ab0  74 66 28 22 5c 6e 5c 6e  22 29 3b 0a 20 20 20 7d  |tf("\n\n");.   }|
00000ac0  0a 0a 20 20 20 70 72 69  6e 74 66 28 22 48 65 72  |..   printf("Her|
00000ad0  65 27 73 20 74 68 65 20  64 61 74 61 20 79 6f 75  |e's the data you|
00000ae0  20 65 6e 74 65 72 65 64  3a 5c 6e 22 29 3b 0a 20  | entered:\n");. |
00000af0  20 20 70 72 69 6e 74 31  28 29 3b 0a 20 20 20 70  |  print1();.   p|
00000b00  72 69 6e 74 66 28 22 50  6c 65 61 73 65 20 70 72  |rintf("Please pr|
00000b10  65 73 73 20 74 68 65 20  52 65 74 75 72 6e 20 6b  |ess the Return k|
00000b20  65 79 3a 5c 6e 22 29 3b  0a 20 20 20 6b 65 79 20  |ey:\n");.   key |
00000b30  3d 20 67 65 74 63 68 61  72 28 29 3b 0a 20 20 20  |= getchar();.   |
00000b40  70 72 69 6e 74 32 28 29  3b 0a 20 20 20 70 72 69  |print2();.   pri|
00000b50  6e 74 66 28 22 50 6c 65  61 73 65 20 70 72 65 73  |ntf("Please pres|
00000b60  73 20 52 65 74 75 72 6e  20 74 6f 20 73 6f 72 74  |s Return to sort|
00000b70  20 74 68 65 20 64 61 74  61 20 61 63 63 6f 72 64  | the data accord|
00000b80  69 6e 67 20 74 6f 20 6e  75 6d 62 65 72 3a 5c 6e  |ing to number:\n|
00000b90  22 29 3b 0a 20 20 20 6b  65 79 20 3d 20 67 65 74  |");.   key = get|
00000ba0  63 68 61 72 28 29 3b 0a  0a 20 20 20 66 6f 72 20  |char();..   for |
00000bb0  28 78 20 3d 20 30 3b 20  78 20 3c 20 4d 41 58 53  |(x = 0; x < MAXS|
00000bc0  49 5a 45 3b 20 78 2b 2b  29 20 7b 0a 20 20 20 20  |IZE; x++) {.    |
00000bd0  20 20 66 6f 72 20 28 79  20 3d 20 78 3b 20 79 20  |  for (y = x; y |
00000be0  3c 20 4d 41 58 53 49 5a  45 3b 20 79 2b 2b 29 20  |< MAXSIZE; y++) |
00000bf0  7b 0a 20 20 20 20 20 20  20 20 20 69 66 20 28 61  |{.         if (a|
00000c00  72 72 61 79 5b 78 5d 20  3e 20 61 72 72 61 79 5b  |rray[x] > array[|
00000c10  79 5d 29 20 7b 0a 20 20  20 20 20 20 20 20 20 20  |y]) {.          |
00000c20  20 20 73 77 61 70 64 61  74 61 28 26 61 72 72 61  |  swapdata(&arra|
00000c30  79 5b 78 5d 2c 20 26 61  72 72 61 79 5b 79 5d 2c  |y[x], &array[y],|
00000c40  20 73 74 72 69 6e 67 73  2c 20 78 2c 20 79 29 3b  | strings, x, y);|
00000c50  0a 20 20 20 20 20 20 20  20 20 7d 0a 20 20 20 20  |.         }.    |
00000c60  20 20 7d 0a 20 20 20 7d  0a 0a 20 20 20 66 6f 72  |  }.   }..   for|
00000c70  20 28 78 20 3d 20 30 3b  20 78 20 3c 20 4d 41 58  | (x = 0; x < MAX|
00000c80  53 49 5a 45 3b 20 78 2b  2b 29 20 7b 0a 20 20 20  |SIZE; x++) {.   |
00000c90  20 20 20 66 6f 72 20 28  79 20 3d 20 78 3b 20 79  |   for (y = x; y|
00000ca0  20 3c 20 4d 41 58 53 49  5a 45 3b 20 79 2b 2b 29  | < MAXSIZE; y++)|
00000cb0  20 7b 0a 20 20 20 20 20  20 20 20 20 69 66 20 28  | {.         if (|
00000cc0  61 72 72 61 79 32 5b 78  5d 2e 6e 75 6d 62 65 72  |array2[x].number|
00000cd0  20 3e 20 61 72 72 61 79  32 5b 79 5d 2e 6e 75 6d  | > array2[y].num|
00000ce0  62 65 72 29 20 7b 0a 20  20 20 20 20 20 20 20 20  |ber) {.         |
00000cf0  20 20 20 73 77 61 70 73  74 72 75 63 74 28 26 61  |   swapstruct(&a|
00000d00  72 72 61 79 32 5b 78 5d  2c 20 26 61 72 72 61 79  |rray2[x], &array|
00000d10  32 5b 79 5d 29 3b 0a 20  20 20 20 20 20 20 20 20  |2[y]);.         |
00000d20  7d 0a 20 20 20 20 20 20  7d 0a 20 20 20 7d 0a 0a  |}.      }.   }..|
00000d30  20 20 20 70 72 69 6e 74  66 28 22 44 61 74 61 20  |   printf("Data |
00000d40  61 66 74 65 72 20 73 6f  72 74 69 6e 67 3a 5c 6e  |after sorting:\n|
00000d50  22 29 3b 0a 20 20 20 70  72 69 6e 74 31 28 29 3b  |");.   print1();|
00000d60  0a 20 20 20 70 72 69 6e  74 66 28 22 50 6c 65 61  |.   printf("Plea|
00000d70  73 65 20 70 72 65 73 73  20 74 68 65 20 52 65 74  |se press the Ret|
00000d80  75 72 6e 20 6b 65 79 3a  5c 6e 22 29 3b 0a 20 20  |urn key:\n");.  |
00000d90  20 6b 65 79 20 3d 20 67  65 74 63 68 61 72 28 29  | key = getchar()|
00000da0  3b 0a 20 20 20 70 72 69  6e 74 32 28 29 3b 0a 20  |;.   print2();. |
00000db0  20 20 70 72 69 6e 74 66  28 22 50 6c 65 61 73 65  |  printf("Please|
00000dc0  20 70 72 65 73 73 20 74  68 65 20 52 65 74 75 72  | press the Retur|
00000dd0  6e 20 6b 65 79 20 74 6f  20 6c 65 61 76 65 20 74  |n key to leave t|
00000de0  68 65 20 70 72 6f 67 72  61 6d 3a 5c 6e 22 29 3b  |he program:\n");|
00000df0  0a 20 20 20 6b 65 79 20  3d 20 67 65 74 63 68 61  |.   key = getcha|
00000e00  72 28 29 3b 0a 0a 20 20  20 63 6c 65 61 6e 75 70  |r();..   cleanup|
00000e10  28 29 3b 0a 7d 0a 0a 76  6f 69 64 0a 63 6c 65 61  |();.}..void.clea|
00000e20  6e 75 70 28 76 6f 69 64  29 0a 7b 0a 20 20 20 69  |nup(void).{.   i|
00000e30  6e 74 20 20 20 78 3b 0a  0a 20 20 20 70 72 69 6e  |nt   x;..   prin|
00000e40  74 66 28 22 46 72 65 65  69 6e 67 20 6d 65 6d 6f  |tf("Freeing memo|
00000e50  72 79 2e 5c 6e 22 29 3b  0a 20 20 20 66 6f 72 20  |ry.\n");.   for |
00000e60  28 78 20 3d 20 30 3b 20  78 20 3c 20 4d 41 58 53  |(x = 0; x < MAXS|
00000e70  49 5a 45 3b 20 78 2b 2b  29 20 7b 0a 20 20 20 20  |IZE; x++) {.    |
00000e80  20 20 66 72 65 65 28 73  74 72 69 6e 67 73 5b 78  |  free(strings[x|
00000e90  5d 29 3b 0a 20 20 20 20  20 20 66 72 65 65 28 61  |]);.      free(a|
00000ea0  72 72 61 79 32 5b 78 5d  2e 73 74 72 69 6e 67 29  |rray2[x].string)|
00000eb0  3b 20 20 20 20 20 0a 20  20 20 7d 0a 7d 0a 0a 76  |;     .   }.}..v|
00000ec0  6f 69 64 0a 70 72 69 6e  74 31 28 76 6f 69 64 29  |oid.print1(void)|
00000ed0  0a 7b 0a 20 20 20 69 6e  74 20 20 20 78 3b 0a 0a  |.{.   int   x;..|
00000ee0  20 20 20 70 72 69 6e 74  66 28 22 44 61 74 61 20  |   printf("Data |
00000ef0  68 65 6c 64 20 69 6e 20  74 68 65 20 74 77 6f 20  |held in the two |
00000f00  73 65 70 61 72 61 74 65  20 61 72 72 61 79 73 3a  |separate arrays:|
00000f10  5c 6e 22 29 3b 0a 20 20  20 70 72 69 6e 74 66 28  |\n");.   printf(|
00000f20  22 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |"---------------|
00000f30  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
00000f40  2d 2d 2d 2d 2d 2d 5c 6e  5c 6e 22 29 3b 0a 20 20  |------\n\n");.  |
00000f50  20 66 6f 72 20 28 78 20  3d 20 30 3b 20 78 20 3c  | for (x = 0; x <|
00000f60  20 4d 41 58 53 49 5a 45  3b 20 78 2b 2b 29 20 7b  | MAXSIZE; x++) {|
00000f70  0a 20 20 20 20 20 20 70  72 69 6e 74 66 28 22 53  |.      printf("S|
00000f80  74 72 69 6e 67 20 25 64  3a 20 25 73 5c 6e 22 2c  |tring %d: %s\n",|
00000f90  20 78 2b 31 2c 20 73 74  72 69 6e 67 73 5b 78 5d  | x+1, strings[x]|
00000fa0  29 3b 0a 20 20 20 20 20  20 70 72 69 6e 74 66 28  |);.      printf(|
00000fb0  22 4e 75 6d 62 65 72 20  25 64 3a 20 25 64 5c 6e  |"Number %d: %d\n|
00000fc0  5c 6e 22 2c 20 78 2b 31  2c 20 61 72 72 61 79 5b  |\n", x+1, array[|
00000fd0  78 5d 29 3b 0a 20 20 20  7d 0a 7d 0a 0a 76 6f 69  |x]);.   }.}..voi|
00000fe0  64 0a 70 72 69 6e 74 32  28 76 6f 69 64 29 0a 7b  |d.print2(void).{|
00000ff0  0a 20 20 20 69 6e 74 20  20 20 78 3b 0a 0a 20 20  |.   int   x;..  |
00001000  20 70 72 69 6e 74 66 28  22 44 61 74 61 20 68 65  | printf("Data he|
00001010  6c 64 20 69 6e 20 74 68  65 20 61 72 72 61 79 20  |ld in the array |
00001020  6f 66 20 73 74 72 75 63  74 75 72 65 73 3a 5c 6e  |of structures:\n|
00001030  22 29 3b 0a 20 20 20 70  72 69 6e 74 66 28 22 2d  |");.   printf("-|
00001040  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
*
00001060  2d 2d 2d 2d 5c 6e 5c 6e  22 29 3b 0a 20 20 20 66  |----\n\n");.   f|
00001070  6f 72 20 28 78 20 3d 20  30 3b 20 78 20 3c 20 4d  |or (x = 0; x < M|
00001080  41 58 53 49 5a 45 3b 20  78 2b 2b 29 20 7b 0a 20  |AXSIZE; x++) {. |
00001090  20 20 20 20 20 70 72 69  6e 74 66 28 22 53 74 72  |     printf("Str|
000010a0  69 6e 67 20 25 64 3a 20  25 73 5c 6e 22 2c 20 78  |ing %d: %s\n", x|
000010b0  2b 31 2c 20 61 72 72 61  79 32 5b 78 5d 2e 73 74  |+1, array2[x].st|
000010c0  72 69 6e 67 29 3b 0a 20  20 20 20 20 20 70 72 69  |ring);.      pri|
000010d0  6e 74 66 28 22 4e 75 6d  62 65 72 20 25 64 3a 20  |ntf("Number %d: |
000010e0  25 64 5c 6e 5c 6e 22 2c  20 78 2b 31 2c 20 61 72  |%d\n\n", x+1, ar|
000010f0  72 61 79 32 5b 78 5d 2e  6e 75 6d 62 65 72 29 3b  |ray2[x].number);|
00001100  0a 20 20 20 7d 0a 7d 0a  0a 76 6f 69 64 0a 73 77  |.   }.}..void.sw|
00001110  61 70 73 74 72 75 63 74  28 44 41 54 41 20 2a 70  |apstruct(DATA *p|
00001120  6f 69 6e 74 31 2c 20 44  41 54 41 20 2a 70 6f 69  |oint1, DATA *poi|
00001130  6e 74 32 29 0a 7b 0a 20  20 20 44 41 54 41 20 20  |nt2).{.   DATA  |
00001140  74 65 6d 70 3b 0a 0a 20  20 20 2f 2a 20 54 68 69  |temp;..   /* Thi|
00001150  73 20 66 75 6e 63 74 69  6f 6e 20 74 61 6b 65 73  |s function takes|
00001160  20 70 6f 69 6e 74 65 72  73 20 74 6f 20 74 77 6f  | pointers to two|
00001170  20 73 74 72 75 63 74 75  72 65 73 20 61 6e 64 20  | structures and |
00001180  73 77 61 70 73 20 74 68  65 69 72 20 63 6f 6e 74  |swaps their cont|
00001190  65 6e 74 73 2e 0a 20 20  20 20 20 20 4e 6f 74 69  |ents..      Noti|
000011a0  63 65 20 68 6f 77 20 74  68 65 79 20 63 61 6e 20  |ce how they can |
000011b0  62 65 20 74 72 65 61 74  65 64 20 61 73 20 73 69  |be treated as si|
000011c0  6e 67 6c 65 20 76 61 72  69 61 62 6c 65 73 20 2d  |ngle variables -|
000011d0  20 74 68 69 73 20 6d 61  6b 65 73 0a 20 20 20 20  | this makes.    |
000011e0  20 20 6d 61 6e 69 70 75  6c 61 74 69 6e 67 20 74  |  manipulating t|
000011f0  68 65 6d 20 61 20 6c 6f  74 20 65 61 73 69 65 72  |hem a lot easier|
00001200  21 20 2a 2f 0a 0a 20 20  20 74 65 6d 70 20 3d 20  |! */..   temp = |
00001210  2a 70 6f 69 6e 74 31 3b  0a 20 20 20 2a 70 6f 69  |*point1;.   *poi|
00001220  6e 74 31 20 3d 20 2a 70  6f 69 6e 74 32 3b 0a 20  |nt1 = *point2;. |
00001230  20 20 2a 70 6f 69 6e 74  32 20 3d 20 74 65 6d 70  |  *point2 = temp|
00001240  3b 0a 7d 0a 0a 76 6f 69  64 0a 73 77 61 70 64 61  |;.}..void.swapda|
00001250  74 61 28 69 6e 74 20 2a  78 2c 20 69 6e 74 20 2a  |ta(int *x, int *|
00001260  79 2c 20 63 68 61 72 20  2a 74 65 6d 70 61 72 72  |y, char *temparr|
00001270  5b 5d 2c 20 69 6e 74 20  76 61 72 31 2c 20 69 6e  |[], int var1, in|
00001280  74 20 76 61 72 32 29 0a  7b 0a 20 20 20 2f 2a 20  |t var2).{.   /* |
00001290  4e 6f 74 69 63 65 20 74  68 65 20 69 6e 63 72 65  |Notice the incre|
000012a0  61 73 65 20 69 6e 20 63  6f 6d 70 6c 65 78 69 74  |ase in complexit|
000012b0  79 20 66 72 6f 6d 20 74  68 65 20 73 77 61 70 73  |y from the swaps|
000012c0  74 72 75 63 74 28 29 20  66 75 6e 63 74 69 6f 6e  |truct() function|
000012d0  20 2d 0a 20 20 20 20 20  20 65 76 65 72 79 20 61  | -.      every a|
000012e0  64 64 69 74 69 6f 6e 61  6c 20 76 61 72 69 61 62  |dditional variab|
000012f0  6c 65 20 69 6e 20 74 68  65 20 64 61 74 61 20 72  |le in the data r|
00001300  65 63 6f 72 64 20 77 6f  75 6c 64 20 6e 65 65 64  |ecord would need|
00001310  20 74 6f 20 62 65 20 64  65 61 6c 74 0a 20 20 20  | to be dealt.   |
00001320  20 20 20 77 69 74 68 20  62 79 20 68 61 6e 64 20  |   with by hand |
00001330  69 6e 20 74 68 69 73 20  66 75 6e 63 74 69 6f 6e  |in this function|
00001340  2c 20 77 68 65 72 65 61  73 20 74 68 65 20 73 77  |, whereas the sw|
00001350  61 70 73 74 72 75 63 74  28 29 20 66 75 6e 63 74  |apstruct() funct|
00001360  69 6f 6e 0a 20 20 20 20  20 20 77 6f 75 6c 64 20  |ion.      would |
00001370  6e 6f 74 20 68 61 76 65  20 74 6f 20 62 65 20 61  |not have to be a|
00001380  6c 74 65 72 65 64 2e 20  2a 2f 0a 0a 20 20 20 69  |ltered. */..   i|
00001390  6e 74 20 74 65 6d 70 3b  0a 20 20 20 63 68 61 72  |nt temp;.   char|
000013a0  20 2a 73 74 72 69 6e 67  74 65 6d 70 3b 0a 0a 20  | *stringtemp;.. |
000013b0  20 20 2f 2a 20 54 68 69  73 20 66 72 61 67 6d 65  |  /* This fragme|
000013c0  6e 74 20 74 61 6b 65 73  20 70 6f 69 6e 74 65 72  |nt takes pointer|
000013d0  73 20 74 6f 20 74 77 6f  20 69 6e 74 65 67 65 72  |s to two integer|
000013e0  73 20 61 6e 64 20 73 77  61 70 73 20 74 68 65 6d  |s and swaps them|
000013f0  20 2a 2f 0a 0a 20 20 20  74 65 6d 70 20 3d 20 2a  | */..   temp = *|
00001400  78 3b 0a 20 20 20 2a 78  20 3d 20 2a 79 3b 0a 20  |x;.   *x = *y;. |
00001410  20 20 2a 79 20 3d 20 74  65 6d 70 3b 0a 0a 20 20  |  *y = temp;..  |
00001420  20 2f 2a 20 54 68 69 73  20 6d 65 73 73 79 20 63  | /* This messy c|
00001430  6f 64 65 20 74 61 6b 65  73 20 61 20 70 6f 69 6e  |ode takes a poin|
00001440  74 65 72 20 74 6f 20 61  6e 20 61 72 72 61 79 20  |ter to an array |
00001450  6f 66 20 70 6f 69 6e 74  65 72 73 20 74 6f 20 73  |of pointers to s|
00001460  74 72 69 6e 67 73 2c 0a  20 20 20 20 20 20 61 6e  |trings,.      an|
00001470  64 20 73 77 61 70 73 20  74 68 65 20 73 74 72 69  |d swaps the stri|
00001480  6e 67 73 20 61 63 63 6f  72 64 69 6e 67 6c 79 2e  |ngs accordingly.|
00001490  20 59 6f 75 20 68 61 76  65 20 74 6f 20 62 65 20  | You have to be |
000014a0  69 6e 20 74 68 65 20 72  69 67 68 74 0a 20 20 20  |in the right.   |
000014b0  20 20 20 66 72 61 6d 65  20 6f 66 20 6d 69 6e 64  |   frame of mind|
000014c0  20 74 6f 20 75 6e 64 65  72 73 74 61 6e 64 20 70  | to understand p|
000014d0  6f 69 6e 74 65 72 73 2e  2e 2e 20 2a 2f 0a 0a 20  |ointers... */.. |
000014e0  20 20 73 74 72 69 6e 67  74 65 6d 70 20 3d 20 74  |  stringtemp = t|
000014f0  65 6d 70 61 72 72 5b 76  61 72 31 5d 3b 0a 20 20  |emparr[var1];.  |
00001500  20 74 65 6d 70 61 72 72  5b 76 61 72 31 5d 20 3d  | temparr[var1] =|
00001510  20 74 65 6d 70 61 72 72  5b 76 61 72 32 5d 3b 0a  | temparr[var2];.|
00001520  20 20 20 74 65 6d 70 61  72 72 5b 76 61 72 32 5d  |   temparr[var2]|
00001530  20 3d 20 73 74 72 69 6e  67 74 65 6d 70 3b 0a 7d  | = stringtemp;.}|
00001540  0a                                                |.|
00001541