user-0 提问: C语言全局变量有时无法写入'\0'，有时又可以，请问怎么破？

回复:
user: user-1
content: 这里似乎看不出明显问题。可能是memcpy之前出的,strcpy, 或者更早，var_val就已经错了。别拘泥在这里

user: user-2
content: 如果对效率不是很敏感的话，可以考虑在拷贝之前memset一下，把内存空间清0

user: user-3
content: 引用 15 楼 insisted_search 的回复:我们看楼主的需求和现象，我们在真的他的问题分析，而不是讨论到编程的习惯。 好吧,我的话可能让大家不舒服了,我其实是无意的调侃,请接受我真诚的抱歉

user: user-4
content: 引用 13 楼 yanran_hill 的回复:[quote=引用 7 楼 insisted_search 的回复:] [quote=引用 6 楼 zhao4zhong1 的回复:] 为什么不用strncpy呢？ 注意：strncpy(t,s,n);t[n]=0; strcpy有什么问题吗？以你的经验。[/quote] 问题大了去了,如果字符串不是以'\0'结尾,那么复制的长度可能超过了从地球到月球:完全符合物理学的测不准原理 $man strcpy [/quote] 我们看楼主的需求和现象，我们在真的他的问题分析，而不是讨论到编程的习惯。

user: user-3
content: 引用 7 楼 insisted_search 的回复:[quote=引用 6 楼 zhao4zhong1 的回复:] 为什么不用strncpy呢？ 注意：strncpy(t,s,n);t[n]=0; strcpy有什么问题吗？以你的经验。[/quote] 问题大了去了,如果字符串不是以'\0'结尾,那么复制的长度可能超过了从地球到月球:完全符合物理学的测不准原理 $man strcpy

user: user-5
content: 引用u_char strtmp[256]; strcpy(strtmp,var_val);  strcpy接受char*， u_char是什么类型？

user: user-6
content: 引用 10 楼 insisted_search 的回复:[quote=引用 8 楼 zhxianbin 的回复:] function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).  To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.  Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied.  Return Value destination is returned. 关键。。。。。[/quote] 用 memcpy

user: user-7
content: strcpy(strtmp,var_val); 这句最好用 memcpy(strtmp, val_val, var_val_len); strtmp[var_val_len] = 0; 来处理。          case COMMIT:              memset(myname, 0, sizeof (myname));              memcpy(myname,strtmp,strlen(strtmp)+1); 这里也类似。  要是想不明白就跟踪一下。

user: user-8
content: 为什么不用strncpy呢？ 注意：strncpy(t,s,n);t[n]=0;

user: user-4
content: 引用 8 楼 zhxianbin 的回复:function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).  To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.  Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied.  Return Value destination is returned. 关键。。。。。

user: user-4
content: 引用 8 楼 zhxianbin 的回复:function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, [color=#FF0000]including the terminating null character (and stopping at that point).  To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.[/color]  Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied.  Return Value destination is returned. 黄色是关键，红色是关键的关键。

user: user-6
content: function <cstring> strcpy char * strcpy ( char * destination, const char * source ); Copy string Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).  To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.  Parameters destination Pointer to the destination array where the content is to be copied. source C string to be copied.  Return Value destination is returned.

user: user-4
content: 引用 6 楼 zhao4zhong1 的回复:为什么不用strncpy呢？ 注意：strncpy(t,s,n);t[n]=0; strcpy有什么问题吗？以你的经验。

user: user-9
content: 你自己把memcpy和strcpy搞混乱了

user: user-4
content: 看了代码没有看出什么问题，不过我想问一下，你是怎么确定这个的： ”全局变量的前一部分字节被新设置的覆盖，而原来的字符串多出来的一部分不会被memcpy复制过来的'\0'所断开“ 是通过snmpget吗？能不能把源码贴一下。

user: user-10
content: 另外你这里u_char应该是unsigned char吧，怎么一会用strcpy,一会用memcpy,有点乱。

user: user-10
content: 单步跟踪看下，别是因为字符串长度超过256了吧。

user: user-11
content: u_char strtmp[256];初始化  ={0}