3

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.

5
  • Try to remove the line free( string ); to see if it's works a bit better... Commented Jan 29, 2017 at 10:00
  • @Joël the call to free is in the testing code, which they are supposed to not modify. Commented Jan 29, 2017 at 10:23
  • A 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. Commented Jan 29, 2017 at 11:18
  • So strictly speaking there are no string arrays at all in the code you show. Commented Jan 29, 2017 at 11:22
  • As Roland said above, I can't change the testing code. It's provided along with a sample output. I saw that the free() was removing the string before I could call it, but I realized thanks to Roland's answer below that I needed to store the string in the array before it was freed in the testcode. Thanks! Commented Jan 29, 2017 at 19:30

2 Answers 2

3

In the setStringArray you are supposed to copy the string (which is the actual sequence of characters). Currently you are only copying a pointer to it.

To do this, you need the functions strlen, malloc and memcpy/strcpy. This function will be more complicated than the other ones.

Alternatively, you can use the function strdup, but that function is not guaranteed to be available on all C platforms.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I got it to work using your suggestions.
0

You should not free the variable "string" as it still points to the data and you are erasing that data; just remove the line of code:

free ( string );

And it will work.

4 Comments

any reason for the downvote? The answer is right, if it wasn't clear just say it and I'll explain better
The call to free is in the testing code, which they are supposed to not modify.
That wasn't written in the question, your answer was right but mine was just a different approach
The question says "we have to come up with the insides of the 4 functions". This implies that they should not modify the rest of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.