1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
char **wordlist;
char **responselist;
int numrows = 3;
int numcols = 255;
void setup()
{
// Create an Array which holds numrows strings of numcols characters
wordlist = (char**)malloc(numrows * sizeof(char*));
responselist = (char**)malloc(numrows * sizeof(char*));
for (int i=0; i<numrows; ++i)
{
wordlist[i] = (char*)malloc(numcols);
responselist[i] = (char*)malloc(numcols);
}
// this part optional. Set each string content to empty string ""
for (int i=0; i<numrows; ++i)
{
*wordlist[i] = '\0';
*responselist[i] = '\0';
}
strcpy(wordlist[0], "First word!");
strcpy(responselist[0], "First Response!");
strcpy(wordlist[numrows-1], "Last word!");
strcpy(responselist[numrows-1], "Last Response!");
}
void increasearray()
{
// Increase the amount of strings the arrays can hold by one.
wordlist = (char**)realloc(wordlist, (numrows+1) * sizeof(char*));
responselist = (char**)realloc(responselist, (numrows+1) * sizeof(char*));
wordlist[numrows] = (char*)malloc(numcols);
responselist[numrows] = (char*)malloc(numcols);
// this part optional. Set each string content to empty string ""
*wordlist[numrows] = '\0';
*responselist[numrows] = '\0';
// lastly, increase value of current array size
++numrows;
}
void cleanup()
{
for (int i=0; i<numrows; ++i)
{
free(wordlist[i]);
free(responselist[i]);
}
free(wordlist);
free(responselist);
}
void printarray(char** array, int size)
{
for (int i=0; i<size; ++i)
{
printf("%3d: %s\n", i+1, array[i]);
}
printf("\n");
}
int main()
{
setup();
printf("1: %s\n2: %s\n\n", wordlist[0], responselist[0]);
printarray(wordlist, numrows);
printarray(responselist, numrows);
//-----------------------------------------
increasearray();
strcpy(wordlist[numrows-1], "Second Word!");
strcpy(responselist[numrows-1], "Second response!");
//-----------------------------------------
increasearray();
strcpy(wordlist[numrows-1], "Apples and Pears");
strcpy(responselist[numrows-1], "Stairs");
//-----------------------------------------
increasearray();
strcpy(wordlist[numrows-1], "Frog and toad");
strcpy(responselist[numrows-1], "Road");
printarray(wordlist, numrows);
printarray(responselist, numrows);
//-----------------------------------------
cleanup();
printf("Done!\n");
getch();
return 0;
}
|