0

I'm running the following C code.

void manual_enter(int * state_count, char * states, char * alphabet, char * start_state, char * accept_states) {
   int counter;

   printf("Please enter the number of states in the DFA. ");
   scanf("%d", state_count);
   states = (char *) malloc(sizeof(char) * (*state_count) );
   printf("\n!");
   counter = 0;
   for(counter = 0; counter < *state_count; counter++) {
      printf("\n!");
      printf("\nPlease enter state: %d", counter);
      scanf("%c", states[counter]);
      printf("%c", states[counter]);
   }

   return;

}

I receive a segfault after the 2nd exclamation point but before the prompt to enter a given state number. I ran in gdb to get a backtrace and this is what I got:

 (gdb) bt
 #0  0x00007fff8b313fda in ?? () from /usr/lib/system/libsystem_platform.dylib
 #1  0x00007fff5fbff360 in ?? ()
 #2  0x00007fff88db1fbb in __fread () from /usr/lib/system/libsystem_c.dylib
 Backtrace stopped: frame did not save the PC

I'm a programming novice, so it may well be that I'm approaching this debugging this all wrong. Any help or tips for how to approach this would be much appreciated. Thanks!

Nick
  • 95
  • 8
  • 3
    The function receives `states` as a parameter. Why are you assigning it with `malloc()`? – Barmar Jan 11 '17 at 22:11
  • 1
    [don't cast the result of malloc in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Jan 11 '17 at 22:11
  • Thanks folks. Easy fix. I'm still somewhat mystified as to why I received the segfault where I did and got the backtrace that I did. But it now seems that the title of the posting is pretty unrelated to the problem/solution. Should I delete the question, retitle, or is there some other way to handle situations like this? – Nick Jan 11 '17 at 22:20
  • 1
    When you cause undefined behavior, the error can happen anywhere. You might not even get an error at all. – Barmar Jan 11 '17 at 22:23
  • 1
    Deleting would be the more reasonable. The code is not good and it will not help others. It is the n*100th incarnation of the same fault. – too honest for this site Jan 11 '17 at 22:26
  • Hokey doke; I wasn't able to delete but I've flagged the question. – Nick Jan 11 '17 at 23:25

1 Answers1

2

The argument corresponding to %c in scanf() has to be a pointer:

  scanf(" %c", &states[counter]);

You should have gotten a compiler warning about this.

You should also put a space before %c so it will skip over whitespace before reading the character. Otherwise, every other state will contain a newline character.

I wonder why you're allocating states when the function receives this as an argument. If the function is supposed to allocate the states array itself and return it to the caller (like it does with state_count), the argument should bechar **states`. Then you would do:

*states = malloc(sizeof(char) * (*state_count));

All the other references to states in the function will also need to be changed to *states.

Barmar
  • 741,623
  • 53
  • 500
  • 612