简体   繁体   English

什么时候必须添加一个空终止符?

[英]When do i have to add a null terminator?

I know that i C , you always have to add a null terminator \\0 so that the processor knows when a word was ended . 我知道我C,您总是必须添加一个空终止符\\0以便处理器知道单词何时结束。

But i get hard time to understand when you have to do it . 但是我很难理解你什么时候要做。 so for example this code works for me without it : 因此,例如,这段代码在没有它的情况下对我有效:

  char connectcmd[50]={0};
  sprintf(connectcmd,"AT+CWJAP=\"%s\",\"%s\"",MYSSID,MYPASS);

How is that possible ? 那怎么可能呢? When do you really have to add them ? 您何时真正需要添加它们?

sprintf always terminates it with null character , so no need to mannually add it. sprintf总是以null character终止它,因此无需手动添加它。

From C99 standard - 从C99标准-

7.21.6.6 The sprintf function 7.21.6.6 sprintf函数

[...]A null character is written at the end of the characters written; [...]在写入的字符的末尾写入一个空字符; it is not counted as part of the returned value. 它不算作返回值的一部分。 If copying takes place between objects that overlap, the behavior is undefined. 如果在重叠的对象之间进行复制,则行为是不确定的。

for example this code works for me without it 例如,没有它,此代码对我有用

It does not (work without it). 它不能( 没有它就可以工作)。

The string literal "AT+CWJAP=\\"%s\\",\\"%s\\"" has a null terminator at the end (like every string literal). 字符串文字"AT+CWJAP=\\"%s\\",\\"%s\\""在结尾处有一个空终止符(就像每个字符串文字一样)。 sprintf copies that null terminator to connectcmd as well. sprintf connectcmd空终止符也复制到connectcmd

When do you really have to add them ? 您何时真正需要添加它们?

When you're manually building a string, or using a library function whose documentation explicitly states that it isn't going to add a terminating null. 当您手动构建字符串或使用库函数(其文档中明确指出不会添加终止null)时。

sprintf writes a null terminated string connectcmd , regardless of its initial contents. sprintf写入一个以null结尾的字符串connectcmd ,而不管其初始内容如何。 This works as long as you don't try to write beyond the bounds of the buffer. 只要您不尝试写超出缓冲区范围的代码,此方法就起作用。

On top of that, when you say this: 最重要的是,当您这样说:

char connectcmd[50]={0};

you initialize all 50 elements of connectcmd to zero, which is the value of the null-terminator \\0 . 您将connectcmd所有50个元素初始化为零,这是空终止符\\0 So it would be null-terminated even if you wrote characters to it manually, as long as you write less than 50 non-null characters. 因此,即使您手动写入字符,只要您写入的字符少于50 ,它也将以null终止。

暂无
暂无

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

相关问题 fread() 不添加空终止符 [C],无法添加,因为我有一个指向指针的指针 - fread() doesn't add null terminator [C], can't add one because I have a pointer to a pointer C:得到错误的输出,这可能与空终止符有关? - C: Getting wrong output which might have to do with null terminator? 在C编程中:当为新字符串mallocing空间时,是否需要考虑'\\ 0'(空终止符) - In C programming: Do I need to consider '\0' (null terminator) when mallocing space for a new string 尝试在字符串末尾添加NUL终止符时,为什么会出现分段错误? - Why do I get a segmentation fault when I try to add a NUL terminator at the end of my string? 当没有更多空间时,编译器如何添加空终止符? - How does the compiler add a null-terminator when there is no more space? 使用strcpy时如何将空终止符添加到char指针 - How to add null terminator to char pointer, when using strcpy 如何在字符串中添加 null 终止符? - How to add a null terminator in string? 如何连接字符串但保留每个单独的 null 终止符? - How do I concatenate strings but keep each individual null terminator? C memset - 优雅地添加一个null终止符 - C memset - elegantly add a null terminator 当我遍历字符指针并检查指针何时到达 null 终止符时,我收到警告 - I am getting a warning when I iterate through the character pointer and check when the pointer reaches the null terminator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM