Looking for some help with my C program. It's something I have to do for school. I think I'm close but still having issues getting the result to print properly. This program consists of 4 .c function files.
For some reason, I can't get the program to output correctly, but I can't figure out why. Maybe someone here can catch what I'm doing wrong. Any tips would be much appreciated. I'm new to programming. The functions and parameters are given to us, and we have to come up with the insides of them.
The functions are supposed to create and access an array of strings using dynamic allocation with pointers and mallocs.
1: createStringArray.c dynamically allocates an array of pointers that will point to strings
#include <stdlib.h>
char** createStringArray(int number){
return malloc((sizeof(char*))*number);
}
2: setStringArray.c stores a string in the array, the string passed into the function must have memory already allocated for it and must be copied into the space. for parameters: array refers to the array allocated in part 1, index is the location where the string will be stored, and string is the string to be passed in.
#include <stdlib.h>
void setStringArray(char** array, int index, char* string){
array[index] = &*string;
}
3: getStringArray.c is supposed to return a string from the array based on the index parameter
char* getStringArray(char** array, int index){
return (char*)array[index];
}
4: freeStringArray.c is supposed to free the array
#include <stdlib.h>
void freeStringArray(char** array, int number){
free(array);
}
We are given a testing mainline to use as well. I have declared the functions in arrayDefns.h as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arrayDefns.h"
#define MAXSIZE 100
int main ( )
{
char **arrayPtrs;
char *string;
char inputStr[MAXSIZE+1];
int number;
int i;
number = 4;
printf ( "arrayPtrs = createStringArray ( %d )\n\n", number );
arrayPtrs = createStringArray ( number );
for ( i=0; i<number; i++ ) {
printf ( "Enter a string: " );
fgets ( inputStr, MAXSIZE, stdin );
inputStr[strlen(inputStr)-1] = '\0';
string = malloc ( sizeof(char) * (strlen(inputStr)+1) );
strncpy ( string, inputStr, strlen(inputStr)+1 );
printf ( "setStringArray ( arrayPtrs, %d, %s )\n", i, string );
setStringArray ( arrayPtrs, i, string );
free ( string );
}
printf ( "\n" );
for ( i=0; i<number; i++ ) {
string = getStringArray ( arrayPtrs, i );
printf ( "%s = getStringArray ( arrayPtrs, %d )\n", string, i );
}
printf ( "\nfreeStringArray ( arrayPtrs, %d )\n", number );
freeStringArray ( arrayPtrs, number );
return(0);
}
Currently the output I'm getting is correct, up until the last part where it is supposed to print "%s = getStringArray" but the %s part is not printing properly.
free( string );to see if it's works a bit better...freeis in the testing code, which they are supposed to not modify.char*is not a string. It might be just pointing the 1st element of a string. So you need to define (implying allocation) the actual "string" separately.