简体   繁体   English

特殊字符字符串

[英]String of special characters

I am required to use string of special characters like: !,",#,~ etc 我需要使用特殊字符的字符串,例如: !,",#,~

If I do : 如果我做 :

char arr[10]  = "''''''''''";
char arr1[10] = "!!!!!!!!!!";
char arr2[10] = "##########";
printf("%s\t%s\t%s\n",arr,arr1,arr2);

then the printf prints the string and some garbage. 然后printf打印字符串和一些垃圾。

Where I am going wrong? 我要去哪里错了?

Characters arrays ( also known as C strings ) are null terminated, so you need an extra index in the array to store the null character. 字符数组(也称为C字符串)以null终止,因此您需要在数组中有一个额外的索引来存储null字符。 If you need a string of 10 characters, you need to create a string which can store 11 characters ( the extra character for the null character '\\0' ) 如果您需要一个包含10个字符的字符串,则需要创建一个可以存储11个字符的字符串(空字符'\\0'的额外字符)

So, change your code to 因此,将您的代码更改为

char arr[11]  = "''''''''''";
char arr1[11] = "!!!!!!!!!!";
char arr2[11] = "##########";
printf("%s\t%s\t%s\n",arr,arr1,arr2);

or even better, as suggested by @TheParamagneticCroissant , you can do 甚至更好,如@TheParamagneticCroissant所建议,您可以

char arr[]  = "''''''''''";
char arr1[] = "!!!!!!!!!!";
char arr2[] = "##########";
printf("%s\t%s\t%s\n",arr,arr1,arr2);

With this, the compiler will find out the length by itself. 这样,编译器将自行找出长度。

You need one byte space more for the nul character: nul字符还需要一个字节空间:

char arr[11]  = "''''''''''";
char arr1[11] = "!!!!!!!!!!";
char arr2[11] = "##########";
printf("%s\t%s\t%s\n",arr,arr1,arr2);

Or simply: 或者简单地:

char arr[]  = "''''''''''";
char arr1[] = "!!!!!!!!!!";
char arr2[] = "##########";
printf("%s\t%s\t%s\n",arr,arr1,arr2);

I hope you got your answer, however, just to clarify things a bit more, for %s format specifier in printf() , as per C11 standard document , chapter §7.21.6.1, 不过,我希望您能得到答案,只是为了进一步说明一下,对于printf() %s格式说明符,按照C11标准文档§7.21.6.1,

s s

If no l length modifier is present, the argument shall be a pointer to the initial element of an array of character type. 如果没有l长度修饰符,则参数应为指向字符类型数组的初始元素的指针。 Characters from the array are written up to (but not including) the terminating null character. 数组中的字符被写入(但不包括)终止空字符。

That means, the argument pointer supplied for %s , should be a pointer to char , having a null in the end to mark the end-of-array . 这意味着,为%s提供的参数指针应该是char的指针,结尾必须有一个null来标记array-of Thus, null-terminated char array are considered strings in C. 因此,以空终止的char数组在C中被视为字符串

Now, coming back to your case, 现在,回到您的情况,

char arr[10]  = "''''''''''";

you have exactly 10 initializer to a 10-element char array. 您有一个10元素的char数组正好有10个初始化程序。 thus, no space for null-termiantor. 因此,没有空值修饰符的空间。 When you sypplu the base address of this array as an argument for printf() to be used for %s , printf() does not know where to stop, thus reading beyond allocated memory which in turn invokes undefined behaviour . 当您sypplu此数组的基地址作为一个参数printf()将被用于%sprintf()不知道在哪里停止,从而读取超出分配的存储器,其依次调用未定义的行为

Solution: 解:

  1. Leave the size allocation to the compiler, best approach. 最好将大小分配留给编译器。

      char arr[] = "''''''''''"; 
  2. or, atleast, allocate enough memory so that the null-termiantor can be accomodated. 或者,至少分配足够的内存,以便可以容纳null-termiantor。

      char arr[11] = "''''''''''"; //10 elements + 1 null 

Please do the same for arr1 and arr2 also. 请对arr1arr2也执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM