首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C:指针指针问题

C:指针指针问题
EN

Stack Overflow用户
提问于 2022-04-02 23:56:45
回答 1查看 74关注 0票数 0

下面是我的代码,我想使用指针来存储字符串。

代码语言:javascript
复制
char **BlankWords(char word[]){
    // take word 'lad' as an example
    // length of it is 3
    // fill in blank: _ad, l_d, la_, _lad, l_ad, la_d, lad_
    // 3 + 4 = 7
    // which means, length of 'lad' + (length of 'lad') + 1
    int strLength = strlen(word);
    char **blank_words = malloc(sizeof(char*) * (2 * strLength + 1));
    assert(blank_words != NULL);
    int i, j, k;
    for (i = 0; i < strLength; i++){
        // allocate memory for each length of the word
        blank_words[i] = calloc(MAX_WORD_LENGTH, sizeof(char));
        assert(blank_words[i] != NULL);
        char temp[MAX_WORD_LENGTH];
        strcpy(temp, word);
        temp[strLength] = '\0';
        temp[i] = '_';
        blank_words[i] = temp;
       // printf("%s\n", blank_words[0]);
    }
    for (j = strLength; j < (2 * strLength + 1); j++){
        // allocate memory for each length of the word
        blank_words[j] = calloc(MAX_WORD_LENGTH, sizeof(char));
        assert(blank_words[j] != NULL);
        char temp[MAX_WORD_LENGTH];
        strcpy(temp, word);
        temp[(strlen(temp) + 1)] = '\0';
        for (k = (strLength - 1); k >= (j - strLength); k--){
            if (k >= 0){
                temp[k + 1] = temp[k];  // in order to insert '_' to the word, then the other letter move back one
            }
        }
        temp[j - strLength] = '_';   // insert '_' to the word
        blank_words[j] = temp;
    }
    return blank_words;
}

下面是输出,每一行都在每个循环之后被覆盖,但在我看来,每一行都不能被覆盖,并且可能存储一个唯一的字符串。

代码语言:javascript
复制
blank_words[0]: lab_
blank_words[1]: lab_
blank_words[2]: lab_
blank_words[3]: lab_
blank_words[4]: lab_
blank_words[5]: lab_
blank_words[6]: lab_

我不知道为什么以前的数据会在每个循环之后被覆盖。我认为,产出应是:

代码语言:javascript
复制
blank_words[0]: _ab
blank_words[1]: l_b
blank_words[2]: la_
blank_words[3]: _lab
blank_words[4]: l_ab
blank_words[5]: la_b
blank_words[6]: lab_
EN

回答 1

Stack Overflow用户

发布于 2022-04-03 01:02:26

正如其他人所说,本地缓冲区在其作用域关闭时消失。由于char**数组指向该类型的缓冲区,因此结果是未定义的行为。您需要使用malloc分配结果字符串。

另一个技巧:您可以通过移动下划线来构建第二组字符串,而不是从头开始创建每个字符串。这一点更简单:

代码语言:javascript
复制
#include <stdlib.h>
#include <string.h>
#include <assert.h>

void *safe_malloc(size_t n) {
  void *r = malloc(n);
  assert(r);
  return r;
}

char *stralloc(char *s) {
  return strcpy(safe_malloc((strlen(s) + 1) * sizeof(char)), s);
}

char **variations(char *s) {
  int len = strlen(s), rp = 0;
  char **r = safe_malloc((2 * len + 1) * sizeof *r);;
  char buf[len + 2];
  strcpy(buf, s); // Copy in case s is a read-only literal.
  for (int i = 0; i < len; ++i) {
    char t = buf[i];               // Remember the i'th char.
    buf[i] = '_';                  // Overwrite with _.
    r[rp++] = stralloc(buf);       // Capture a copy.
    buf[i] = t;                    // Replace original char.
  }
  buf[0] = '_';                    // Make the 1st char _.
  strcpy(buf + 1, s);              // Copy the rest after.
  r[rp++] = stralloc(buf);         // Capture a copy.
  for (int i = 0; i < len; ++i) {
    buf[i] = buf[i + 1];           // Overwrite _ with following char.
    buf[i + 1] = '_';              // Move the _ up one position.
    r[rp++] = stralloc(buf);       // Capture a copy.
  }
  return r;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71722006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档