Look-and-say sequence generator, segfault during 3rd iteration of forloop

This is a discussion on Look-and-say sequence generator, segfault during 3rd iteration of forloop within the C Programming forums, part of the General Programming Boards category; Hello everybody, I'm a bit of a newbie with c. I've written the following program to generate a look-and-say sequence ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Look-and-say sequence generator, segfault during 3rd iteration of forloop

    Hello everybody,

    I'm a bit of a newbie with c. I've written the following program to generate a look-and-say sequence (Look-and-say sequence - Wikipedia, the free encyclopedia). It should ask for a number and then generate that number of items in the sequence. However, it doesn't generate more than 2 numbers, after that it just segfaults.

    Does anybody have a clue where I'm going wrong with this one?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAXLENGTH 1000
    
    void gen_next_conway(char *cur_conway, char *new_conway, int max);
    void getline(char *line, int max);
    
    main()
    {
      char conway[MAXLENGTH] = "312211";
      char newconway[MAXLENGTH];
      char *curconway = conway;
      char c_count[10];
      int count, i;
    
      printf("Hoeveel conway nummers wil je zien? ");
      getline(c_count, 10);
      count = atoi(c_count);
    
      for (i = 0; i < count; i++)
      {
        printf("Current conway at %d: %s\n", i, curconway);
        printf("Newconway at %d: %s\n", i, newconway);
        gen_next_conway(curconway, newconway, MAXLENGTH);
        printf("%s\n", newconway);
        curconway = newconway;
      }
    
    }
    
    void gen_next_conway(char *cur_conway, char *new_conway, int max)
    {
      int digit_count;
      char cur_digit;
      char *digitcounter;
    
      while (*cur_conway)
      {
        cur_digit = *cur_conway;
    
        for(digit_count = 0; *cur_conway == cur_digit; digit_count++, cur_conway++)
          ;
    
        sprintf(new_conway, "%d", digit_count);
        new_conway++;
        *new_conway = cur_digit;
        new_conway++;
      }
      
      *new_conway = '\0';
    }
    
    void getline(char *line, int max)
    {
      char c;
      int i = 0;
    
      while ((c = getchar()) != EOF && c != '\n' && i < max)
      {
        *line = c;
        line++;
        i++;
      }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    atoi requires that you provide it a string. Your getline function does not itself produce a string, but rather a series of characters which are not null terminated. The only reason it is null terminated is because of your initial declaration of the variable, which zero fills all uninitialized elements.

    However...

    If they enter a three digit number, and hit enter, it's going to put in "123", and beyond that, it will have the rest of "211" from your initialization. I'm pretty sure that's not what you want.

    Start there.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21