【问题标题】:Random word from a character set segmenation fault来自字符集分段错误的随机词
【发布时间】:2021-01-19 00:18:47
【问题描述】:

我想从给定集合中的随机字符数中获取一个单词,但是出现了分段错误。如何摆脱它?

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

char* getrand() {
  char* symbols[] = {"a,", "b", "с", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
  char* letter;
  char* words;
  for (int i = 0; i < rand() % 10; i++) {
   letter = symbols[rand() % 26];
   words = strcat(letter, symbols[rand() % 26]);
  }
  return words;
}

int main(void) {
  srand(time(NULL));
  char* new_word;
  new_word = getrand();
  printf("%s\n", new_word);
  return 0;
}

【问题讨论】:

  • 为了造一个词,首先需要有一个空格来放这个词。strcat 不会产生空格,请阅读documentation
  • 即变量“words”需要使用malloc吗?
  • @RumeOne 你的第一个符号是"a," 还是"a"?可能只是一个错字(我的意思是逗号)?
  • 是的,这是一种可能性。您还需要更改呼叫strcat 的方式。

标签: arrays c string pointers memory-management


【解决方案1】:

通过为您的字符串分配内存,无论是静态的还是动态的(在这里您确实不需要,但仅出于练习目的,我会提到它)。

在我看来,你把事情复杂化了。假设你的话实际上有字母(所以"a,"是一个错字,你真的想写"a"),那么你可以这样做:

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

#define MAX_LEN 10
#define SYMBOLS_LEN 26

void getrand_word(char* word, int word_max_len) {
  char symbols[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  char letter;
  for (int i = 0; i < rand() % word_max_len; i++) {
   letter = symbols[rand() % SYMBOLS_LEN];
   word[i] = letter;
  }
}

int main(void) {
  srand(time(NULL));
  char word[MAX_LEN] = {0};
  getrand_word(word, MAX_LEN);
  printf("%s\n", word);
  return 0;
}

输出:

cik

在这里我静态分配了一个名为word 的字符串数组,大小为MAX_LEN,等于10,因为我们知道我们最多会随机选择一个字母(符号)9 (10 - 1) 次。

所以我们的单词最多可以有 9 个字母,但是我们需要在字符串数组中多出一个槽,以便将 NULL 终止符存储到其中,因此我们需要字符串数组的大小为 10。

然后我将这个字符串数组传递给getrand_word(),以便随机填充我的字符串。

在该方法中,我有一个字符数组(而不是像您的示例中那样的字符串),因为符号(字母)是一个字符,而不是一个字符串。

for 循环最多会运行(word_max_len - 1 次),因为rand() % word_max_len 将整数生成为 [0, word_max_len)。在那里我们随机选择一个数字来索引我们的symbols 数组。我们希望能够从头到尾选择任何字母,所以我们需要rand() % SYMBOLS_LEN。阅读更多How to generate a random int in C?

选择字母后,只需将其附加到字符串中即可,使用循环计数器作为字符串的索引。

【讨论】:

    【解决方案2】:

    我做了一些小的修改:

    • 为单词分配存储空间
    • 使用后释放分配的字
    • 正确使用strcat
    • 确保它不会生成长度为零的单词
    • 修正符号初始化程序中的错字
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    const int MAX_WORD = 10;
    
    char *get_random_word(void) {
      const char *symbols[] = {
        "a", "b", "с", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
        "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
      };
      char *words = calloc(MAX_WORD + 1, sizeof(char));
      if (!words) {
        return NULL;
      }
    
      for (int ii = 0; ii < 1 + (rand() % MAX_WORD); ii++) {
        strcat(words, symbols[rand() % 26]);
      }
    
      return words;
    }
    
    int main(void) {
      srand(time(NULL));
      char *new_word = get_random_word();
      if (new_word) {
        printf("%s\n", new_word);
        free(new_word);
        return 0;
      } else {
         return 1;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 2013-11-02
      相关资源
      最近更新 更多