小编won*_*and的帖子

为什么字符串终止不会发生在null字符处

为什么它在第二个字符串中打印空字符?

声明字符数组应该在结尾处自动添加空字符.这取决于编译器吗?

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

bool ChckStrng(char *str);

void main()
{
  char a[] = "hello";
  char b[] = "abc";



  printf("a:%d\n", ChckStrng(a));
  printf("b:%d\n", ChckStrng(b));

}

bool ChckStrng(char *str)
{
  int count[26];

  while(str != NULL)
  {
    printf(":%d:\n", *str - 'a');
    if(++count[*str - 'a'] > 1)
     return false;
    str = str + 1;
  }
  printf("end\n");
  return true;
}
Run Code Online (Sandbox Code Playgroud)

输出1:

:7 :: 4 :: 11:11:a:0

:0 :: 1 :: 2:: - 97 :: -97:b:0

c string null char

2
推荐指数
1
解决办法
70
查看次数

标签 统计

c ×1

char ×1

null ×1

string ×1