库函数strncpy,字符数组与指针。还有字符数组结尾是'\0'

china_ssl 2011-08-31 10:35:17
我感觉和我想的不一样啊

char *strncpy(char *s, char *ct, int n);
char *strncat(char *s, char *ct, int n);
int strncmp(char *cs, char *ct, int n);

char *strncpy(char *s, char *ct, int n) {
char *sign = s;
while(n-- > 0)
if(*s++ = *ct) ct++;
return sign;
}

char *strncat(char *s, char *ct, int n) {
char *sign = s;
while(*s)
s++;
while(n-- && (*s++ = *ct++))
;
return sign;
}


为什么后面的不是用'\0'而是其它什么的呢,windows下好像不确定,linux下确是冲头开始。。。。

比如我这样测试

#include<stdio.h>

#include "strncpy.c"

int getchar(void);

int main() {
int i;
char c[12] = "ca3456";
char s[4] = "zsev";
//char *z = c;
strncat(c, s, 4);
//if(*(s+3) == 'v')
//printf("%c\n", *(s+4));
for(i = 0; i < 12; i++)
printf("%c\t\n", *(c+i));

return 0;
}


结果windows下输出:

c
a
3
4
5
6
z
s
e
v
?
4
linux下输出:

a
c
3
4
5
6
z
s
e
v
a
c
这是为什么啊???
按我的本意是不足n位的全用EOF的啊ct指向的字符数组结尾应该是'\0'啊,也就是如果ct指向的字符数组不足n位就把'\0'复制给s指向的数组以补齐n位啊。
...全文
346 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
还有噢,我玩魂守和斧王很厉害的。
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
我懂了,还是因为我用的是s[4]空间给限制了。
赵4老师 2011-08-31
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖;
要迷信CPU、编译器、调试器、运行结果。
赵4老师 2011-08-31
  • 打赏
  • 举报
回复
The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.

The strncat function appends, at most, the first count characters of strSource to strDest. The initial character of strSource overwrites the terminating null character of strDest. If a null character appears in strSource before count characters are appended, strncat appends all characters from strSource, up to the null character. If count is greater than the length of strSource, the length of strSource is used in place of count. The resulting string is terminated with a null character. If copying takes place between strings that overlap, the behavior is undefined.

赵4老师 2011-08-31
  • 打赏
  • 举报
回复
VC调试(TC或BC用TD调试)时按Alt+8、Alt+6和Alt+5,打开汇编窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应内存和寄存器变化,这样过一遍不就啥都明白了吗。
(Linux或Unix下可以在用GDB调试时,看每句C对应的汇编并单步执行观察相应内存和寄存器变化。)
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
我实验了,char s[5] = "zsev";是可以的。
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jackyjkchen 的回复:]

请改成
char s[] = "zsev";
你都强制指定4了,哪有空间给你放\0
[/Quote]
噢,好的,我知道了,原来是这样的啊。那如果我char s[5] = "zsev";这样不知道行不行。
rendao0563 2011-08-31
  • 打赏
  • 举报
回复
你的理解有问题吧.

你说的可能是这种情况 char szBuf[] = "zsev";

[Quote=引用 4 楼 china_ssl 的回复:]

我再问个问题啊,书上说字符数组都会多比实际多出一位'\0'作为结束,但我做实验发现

C/C++ code

int main() {
char s[4] = "zsev";
if(s[4] == '\0')
printf("%s\n", "asd");
else
printf(……
[/Quote]
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jackyjkchen 的回复:]

这几个函数指定了最大长度,如strncat中间有0就停止,但不保证末尾
[/Quote]
请看看我写的

char *strncpy(char *s, char *ct, int n) {
char *sign = s;
while(n-- > 0)
if(*s++ = *ct) ct++;
return sign;
}


我是while循环一定要循环n次啊,就算*ct == EOF了也继续循环的啊,只不过那时候ct就一直指向EOF那个了,应该还是继续*s++ = *ct的啊,只是ct不++了。
jackyjkchen 2011-08-31
  • 打赏
  • 举报
回复
请改成
char s[] = "zsev";
你都强制指定4了,哪有空间给你放\0
china_ssl 2011-08-31
  • 打赏
  • 举报
回复
我再问个问题啊,书上说字符数组都会多比实际多出一位'\0'作为结束,但我做实验发现


int main() {
char s[4] = "zsev";
if(s[4] == '\0')
printf("%s\n", "asd");
else
printf("%s\n","222");


return 0;
}

发现这样输出的是222

luciferisnotsatan 2011-08-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jackyjkchen 的回复:]

这几个函数指定了最大长度,如strncat中间有0就停止,但不保证末尾
[/Quote]
++
jackyjkchen 2011-08-31
  • 打赏
  • 举报
回复
这几个函数指定了最大长度,如strncat中间有0就停止,但不保证末尾
mymtom 2011-08-31
  • 打赏
  • 举报
回复
这几个函数都不保证目标字符串以'\0'结尾。

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧