【问题标题】:How to print a char array in C through printf? [closed]如何通过printf在C中打印一个char数组? [关闭]
【发布时间】:2018-10-23 01:03:17
【问题描述】:

这会导致分段错误。 需要纠正什么?

int main(void)
{
    char a_static = {'q', 'w', 'e', 'r'};
    char b_static = {'a', 's', 'd', 'f'};

    printf("\n value of a_static: %s", a_static);
    printf("\n value of b_static: %s\n", b_static);
}

【问题讨论】:

标签: c arrays char printf


【解决方案1】:

贴出的代码不正确:a_staticb_static 应该定义为数组。

有两种方法可以更正代码:

  • 您可以添加空终止符以使这些数组成为正确的 C 字符串:

    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r', '\0' };
        char b_static[] = { 'a', 's', 'd', 'f', '\0' };
    
        printf("value of a_static: %s\n", a_static);
        printf("value of b_static: %s\n", b_static);
        return 0;
    }
    
  • 或者,printf 可以使用精度字段打印非空终止的数组的内容:

    #include <stdio.h>
    
    int main(void) {
        char a_static[] = { 'q', 'w', 'e', 'r' };
        char b_static[] = { 'a', 's', 'd', 'f' };
    
        printf("value of a_static: %.4s\n", a_static);
        printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static);
        return 0;
    }
    

    . 之后给出的精度指定从字符串输出的最大字符数。它可以以十进制数或* 的形式给出,并在char 指针之前作为int 参数提供。

【讨论】:

    【解决方案2】:

    这会导致分段错误。 ? 因为下面的陈述

    char a_static = {'q', 'w', 'e', 'r'};
    

    a_static 应该是 char array 以容纳多个字符。让它像

     char a_static[] = {'q', 'w', 'e', 'r','\0'}; /* add null terminator at end of array */
    

    b_static 也是如此

    char b_static[] = {'a', 's', 'd', 'f','\0'};
    

    【讨论】:

    • 啊!谢谢你的错。
    • 您还应该在 char 数组中添加 '\0' 字符以防止溢出。
    【解决方案3】:

    你需要使用数组而不是声明

    a_static
    b_static
    

    作为变量

    所以它看起来像这样:

    int main()
    {
      char a_static[] = {'q', 'w', 'e', 'r','\0'};
      char b_static[] = {'a', 's', 'd', 'f','\0'};
      printf("a_static=%s,b_static=%s",a_static,b_static);
      return 0;
    }
    

    【讨论】:

      【解决方案4】:

      问题是您使用的是 C 样式字符串,而 C 样式字符串以零结尾。 例如,如果您想使用 char 数组打印“alien”:

      char mystring[6] = { 'a' , 'l', 'i', 'e' , 'n', 0}; //see the last zero? That is what you are missing (that's why C Style String are also named null terminated strings, because they need that zero)
      printf("mystring is \"%s\"",mystring);
      

      输出应该是:

      我的字符串是“外星人”

      回到你的代码,它应该是这样的:

      int main(void) 
      {
        char a_static[5] = {'q', 'w', 'e', 'r', 0};
        char b_static[5] = {'a', 's', 'd', 'f', 0}; 
        printf("\n value of a_static: %s", a_static); 
        printf("\n value of b_static: %s\n", b_static); 
        return 0;//return 0 means the program executed correctly
      }
      

      顺便说一句,你可以使用指针代替数组(如果你不需要修改字符串):

      char *string = "my string"; //note: "my string" is a string literal
      

      你也可以用字符串字面量初始化你的 char 数组:

      char mystring[6] = "alien"; //the zero is put by the compiler at the end 
      

      另外:对 C 样式字符串(例如 printf、sscanf、strcmp、strcpy 等)进行操作的函数需要零才能知道字符串的结束位置

      希望您从这个答案中学到了一些东西。

      【讨论】:

      • 这在各个地方都有问题并且部分错误。首先:指针从不替代数组。如果一个人想用破碎的代码和缺乏这样的基础来回答问题,他们至少应该把它弄得非常正确,这样 OP 就不需要购买一本教科书来正确学习语言,这将是正确的方法。
      猜你喜欢
      • 2021-01-13
      • 2013-12-07
      • 1970-01-01
      • 2012-10-29
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多