关于strncpy的用法问题?
#include <bits/stdc++.h>using namespace std;
int main(void)
{
char str1[]="1223123245454513231";
char str2;
strncpy(str2,str1,10);
cout<<str2<<endl;
return 0;
}
为什么这样用会输出:12231232451223123245454513231呢? 应该是不同的编译器会有所不同吧
数组越界了! {:10_249:}贴个函数的源码一起研究研究 理想小青年 发表于 2018-5-7 09:01
贴个函数的源码一起研究研究
/* Copyright (C) 1991-2017 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>.*/
#include <string.h>
#undef strncpy
#ifndef STRNCPY
#define STRNCPY strncpy
#endif
char *
STRNCPY (char *s1, const char *s2, size_t n)
{
size_t size = __strnlen (s2, n);
if (size != n)
memset (s1 + size, '\0', n - size);
return memcpy (s1, s2, size);
}
libc_hidden_builtin_def (strncpy)
人造人 发表于 2018-5-7 16:48
就你优秀 理想小青年 发表于 2018-5-8 09:01
就你优秀
^_^ 我来说声话 打出来的结果 应该是str1[]里的前十个数 ,不用质疑,如果有别的答案应该是类型宽度 不同, 就像32位 和 64位 一样 长度不同
页:
[1]