写入 char* 数组的 char 会发生某种变化

标签 c char buffer

这应该是一个简单的函数(它计算字符串中唯一字符的数量),但我遇到了一个奇怪的问题。请注意,我的代码仅使用 ASCII 字母 a-z 和 A-Z。

int unique_chars(char* my_str) {
//printf("starting unique_chars\n");
  char seen_buffer[52]; // max 52 letters a-z & A-Z
  int seen_count = 1; // not ever expecting my_str to be NULL 
  int i, j;
  char next;
//printf("first char is %c\n", my_str[0]);
  seen_buffer[0] = my_str[0]; // first char must be unique

  for (i=1; i<strlen(my_str); i++) { // walk along the rest of my_str
    next = my_str[i];

    if (next >= 97) {
      next = next - 32; // the next char will always be capital, for convenience
    }

    for (j=0; j<seen_count; j++) { // compare next to all the unique chars seen before
//printf("current char is %c, checking against %c\n", next, seen_buffer[j]);
      if ((next==seen_buffer[j]) || (next+32==seen_buffer[j])) {
//printf("breaking\n");
        break; // jump to the next char in my_str if we find a match
      }
      if (j==seen_count-1) { // at this point, we're sure that next hasn't been seen yet
//printf("new unique char is %c\n", next);
        seen_count++;
        seen_buffer[seen_count] = next;
//printf("new char val is %c, should be %c\n", seen_buffer[seen_count], next);
        break;
      }
    }
  }
  return seen_count;
}

int main(int argc, char* argv[]){
  char* to_encode = argv[1];
  printf("unique chars: %d\n", unique_chars(to_encode));
}

当我使用某些字符串调用时,我得到的结果不正确。例如,尝试:

./a.out gghhiijj

产生(printf 未注释):

starting unique_chars
first char is g
current char is G, checking against g
breaking
current char is H, checking against g
new unique char is H
new char val is H, should be H
current char is H, checking against g
current char is H, checking against 
new unique char is H
new char val is H, should be H
current char is I, checking against g
current char is I, checking against 
current char is I, checking against H
new unique char is I
new char val is I, should be I
current char is I, checking against g
current char is I, checking against 
current char is I, checking against H
current char is I, checking against H
new unique char is I
new char val is I, should be I
current char is J, checking against g
current char is J, checking against 
current char is J, checking against H
current char is J, checking against H
current char is J, checking against I
new unique char is J
new char val is J, should be J
current char is J, checking against g
current char is J, checking against 
current char is J, checking against H
current char is J, checking against H
current char is J, checking against I
current char is J, checking against I
new unique char is J
new char val is J, should be J

所以我一直在我的 saw_buffer 中收到重复项,因为一些空白字符存储在那里,而不是应该在那里的字母字符!然而,当我在写入 saw_buffer 后立即进行比较(即新的字符值是 %c,应该是 %c\n)时,会显示正确的字符!

感谢任何帮助!

最佳答案

这里有一个相差一的错误:

    seen_count++;
    seen_buffer[seen_count] = next;

第一个字符进入 seen_buffer[0]seen_count设置为 1。这意味着下一个新字符进入 seen_buffer[2]之后seen_count增加到 2。seen_buffer[1] 中没有任何内容。 (这是您在 printfs 中不断看到的空白字符),以及根据 seen_buffer 检查字符时,您永远不会检查刚刚输入的最后一个字符。

交换这些行就可以了。

关于写入 char* 数组的 char 会发生某种变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15126873/

相关文章:

c - 将内存分配给作为函数内部参数的指针

c++ - 为什么我不能在 char * 和 unsigned char * 之间进行静态转换?

c - 如何在 C 中将 int 转换为字符串 (char*)

search - Vim:vim 实例之间的持久搜索缓冲区(在 Ubuntu 中)

c - zlib:如何标注 avail_out

c - 如何清除 C 中的输入缓冲区?

c - 将输出延迟到 EOF 而不是换行符

java - 通过 AppMessage 在 Pebble 上显示图像

java - 将pdf内容写入java.io.Writer

c - 程序流卡在函数中