Software Engineering
Programming in C
Class Syllabus #8
Dynamic Allocation, Arrays of pointers
/*strArray.h*/
/*This module performs operations on an array of char* . Note that
it is the responsibility of the user to call initArray() before doing
anything else.
The convention that the array either contains a pointer to a string
or a NULL pointer.
In all functions, the set of parameters (char ** stringArray, int length)
means the pointer to the beginging of the char* array, and the length
of the char* array.*/
void initArray(char ** stringArray, int length);
//set All pointers in Array to NULL
//note that, this function may not be called after memory has
//been allocated
void clearArray(char ** stringArray, int length);
//clear all entries in the Array, (freeing memory)
int numInArray(char ** stringArray, int length);
//return the number of strings that the array points to
//(this is the number of non-NULL pointers)
void stringToArray(char ** stringArray, int position, char * sourceString);
//copy sourceString to the array, into the spot: position
//note that it is the user's responsiblity that position is valid
void removeElement(char ** stringArray, int position);
//removes the string in position, users responsibility that
//position is valid index
/*strArray.c*/
#include "strArray.h"
#include <stdlib.h>
#include <string.h> //for strlen()
void initArray(char ** strA, int len)
{
int i;
for(i=0; i<len; i++)
strA[i]=NULL; //note that if strA[i] had a pointer to memory
//than this memory is lost and a leak is created
}
void clearArray(char ** strA,int len)
{
int i;
for(i=0;i<len;i++)
{
free(strA[i]);
strA[i]=NULL;
}
}
int numInArray(char ** strA,int len)
{
int i;
int num=0;//the number of strings pointed to by the the array
for(i=0;i<len;i++)
num += (strA[i] != NULL) ;
return num;
}
void stringToArray(char ** strA, int pos, char * pc)
{
char * pdest;
free(strA[pos]);
//free array and allocate memory for new string + 1 for '\0'
pdest=strA[pos]=(char *) malloc( (strlen(pc) + 1) * sizeof(char) );
while( *pc ) //this loop is strcpy()
*(pdest++) = *(pc++);
*pdest='\0'; //seel off the string
}
void removeElement(char ** strA, int pos)
{
free(strA[pos]);
strA[pos]=NULL;
}
/*main.c*/
#include "strArray.h"
#include <stdio.h>
#define NUM_STRINGS 10
void main()
{
int i;
char * stringArray[NUM_STRINGS];
initArray(stringArray,NUM_STRINGS);
stringToArray(stringArray,0,"This is string in position zero");
stringToArray(stringArray,3,"This is string in position three");
stringToArray(stringArray,6,"This is string in position six");
stringToArray(stringArray,9,"This is string in position nine");
printf("Number of strings in array: %d\n",
numInArray(stringArray,NUM_STRINGS) );
printf("Now print the values of the array:\n");
for(i=0; i < NUM_STRINGS; i++)
printf("Element %d:%s\n",i,stringArray[i]);
}
OUTPUT:
Number of strings in array: 4
Now print the values of the array:
Element 0:This is string in position zero
Element 1:(null)
Element 2:(null)
Element 3:This is string in position three
Element 4:(null)
Element 5:(null)
Element 6:This is string in position six
Element 7:(null)
Element 8:(null)
Element 9:This is string in position nine
Additions and modifications to the above program:
- Use a struct {char ** stringArray; int length;}; to allow the same functionality.
Introduce argv[], argc
Write a running statistics function using a static variable:
typedef enum {average, sum, mode} STAT_COMMAND;
float statistic(float * dataArray, int length, STAT_COMMAND com);
String functions from string.h, stdio.h, stdlib.h soted by category
int strlen(const char * string);char * strcat(char * strDestination, const char * strSource);
char * strncat(char * strDest, const char * strSource, int count);
int strcmp(const char * string1, const char * string2);
int stricmp(const char * string1, const char * string2);
int strncmp(const char * string1, const char * string2, int count);
int strnicmp(const char * string1, const char * string2, int count);
char * strcpy(char * strDestination, const char * strSource);
char * strncpy(char * strDest, const char *strSource, int count);
char * strdup(const char * strSource);
char * strlwr(const char * string);
char * strupr(const char * string);
char * strset(const char * string, char c);
char * strnset(const char * string, char c, int count);
char * strchr(const char * string, char c);
char * strrchr(const char * string, char c);
int strspn(const char * string, const char * strCharSet);
int strcpsn(const char * string, const char * strCharSet);
char * strstr(const char * string, const char * strCharSet);
char * strtok(char * strToken, const char * strDelimit);
int sscanf(const char * buffer, const char * format [,argument] … ); <stdio.h>
long strtol(const char * nptr, char ** endptr, int base);
double strtod(const char * nptr, char ** endptr);
unsigned long strtoul(const char * nptr, char ** endptr, int base);
double atof(const char * str); <stdlib.h>
int atoi(const char * str); <stdlib.h>
long atol(const char * str); <stdlib.h>
1