简体   繁体   中英

Arrays of strings with pointers in C

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM