8

So I am getting re-acquainted with C, and this one concept has me particularly stuck.

The goal is to create a dynamically allocated array of strings. I have this done, first creating a null array and allocating the appropriate amount of space for each string entered. The only problem is, when I try to actually add a string, I get a seg fault! I can't figure out why, I have a hunch that it is from improper allocation as I can't see anything wrong with my strcpy function.

I have looked exhaustively on this site for an answer, and I have found help, but can't quite close the deal. Any help you can provide would be greatly appreciated!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
  int count = 0; //array index counter
  char *word; //current word
  char **array = NULL;


  char *term = "q"; //termination character
  char *prnt = "print";

  while (strcmp(term, word) != 0)
{
  printf("Enter a string.  Enter q to end.  Enter print to print array\n");
  // fgets(word, sizeof(word), stdin); adds a newline character to the word.  wont work in this case
  scanf("%s", word);

  //printf("word: %s\nterm: %s\n",word, term);

  if (strcmp(term, word) == 0)
    {
    printf("Terminate\n");
    } 

  else if (strcmp(prnt, word) == 0)
  {
    printf("Enumerate\n");

    int i;

    for (i=0; i<count; i++)
    {
      printf("Slot %d: %s\n",i, array[i]);
    }

  }
  else
  {
    printf("String added to array\n");
    count++;
    array = (char**)realloc(array, (count+1)*sizeof(*array));
    array[count-1] = (char*)malloc(sizeof(word));
    strcpy(array[count-1], word);
  }

}

  return ;

}

1 Answer 1

7

word has no memory allocated to it. Your program in its current form is trampling over unallocated memory as users enter words into your program.

You should guesstimate how large your input would be and allocate the input buffer like this:

char word[80];  // for 80 char max input per entry
Sign up to request clarification or add additional context in comments.

5 Comments

Ah!! How silly of me, it always seems to be the minute details that get overlooked. This fixed helped like a charm. Thank you so much!
also, fgets(word, sizeof(word), stdin); commented out in the OP's code, is wrong because sizeof operator doesn't count the number of characters of word but instead returns the size of the word variable's type, and since word is a pointer sizeof(word) will give the size of a pointer, i.e. sizeof(char *). If the OP uses this solution however, sizeof operator is well aplied in that case.
@colinmcp Oh, and also, prevent buffer overflow doing scanf("%79s", word); the number being the size of the array minus 1, since you should account for the null termination byte.
@iharob thanks for the tip, I originally commented it out because it gave an unnecessary newline character, but this is good to know.
Also note that in the first iteration your while condition is comparing term against an uninitialized string. Declaring char word[80] = "" should fix that, or you can redesign your main loop (e.g. using an infinite loop and breaking it in the termination test to avoid testing the termination condition twice).

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.