可能重复:
为什么要使用strncpy而不是strcpy?
我正在读一本关于计算机/加密等的书.通常作者使用的东西,strncpy(dest, src, strlen(src));而不是strcpy(dest, src);它对我来说没什么意义..好吧,我是一个专业的程序员.
问题是:它真的有一个真正的区别吗?真正的应用使用这样的东西?
所以我想编写一个程序来遍历目录并将文件名添加到名为“filesList”的字符串数组中。但问题是,当它完成时,数组中的每个元素都是目录中最后一个文件的名称。这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int main(int argc, char *argv[])
{
int n=0, i=0;
DIR *d;
struct dirent *dir;
d = opendir(argv[1]);
//Determine the number of files
while((dir = readdir(d)) != NULL) {
if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") )
{
} else {
n++;
}
}
rewinddir(d);
char *filesList[n];
//Put file names into the array
while((dir = readdir(d)) != NULL) {
if ( !strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..") )
{}
else {
filesList[i]= dir->d_name; …Run Code Online (Sandbox Code Playgroud) 我知道strncpy是一个更安全的版本,strcpy如说在这里.
但是,当我想要复制src到dst并且dst不是一个干净的缓冲区时,我会收到不需要的结果,这可以通过以下方式避免strcpy:
char *p = "123";
char a[10] = "aaaaaa";
strncpy(a,p,strlen(p));
printf("%s\n", a); // 123aaa
strcpy(a,p);
printf("%s\n", a); // 123 <- desired output, as the trailing a's are garbage
Run Code Online (Sandbox Code Playgroud)
在我的实际情况中,我知道strlen(src) < sizeof(dst)(至少,如果不是这样,程序会很快崩溃),所以我可以安全地strcpy.
但是,如果strncpy是我应该使用的,那么我必须在之后添加dst[strlen(src)] = '\0'以避免垃圾(或者更好的是,事先启动缓冲区吗?)?
我想知道strncpy考虑到max一些字符,是否有一种更干净,更有效的方法来做以下事情.我觉得自己过度了.
int main(void)
{
char *string = "hello world foo!";
int max = 5;
char *str = malloc (max + 1);
if (str == NULL)
return 1;
if (string) {
int len = strlen (string);
if (len > max) {
strncpy (str, string, max);
str[max] = '\0';
} else {
strncpy (str, string, len);
str[len] = '\0';
}
printf("%s\n", str);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我需要将字符串“ on”替换为“ in”,strstr()函数返回一个指向字符串的指针,所以我想将新值分配给该指针可以工作,但没有成功
#include <stdio.h>
#include <string.h>
int main(void) {
char *m = "cat on couch";
*strstr(m, "on") = "in";
printf("%s\n", m);
}
Run Code Online (Sandbox Code Playgroud) 这是我做的:
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "longname";
printf("Name = %s \n",name);
strcpy(name,"evenlongername");
printf("Name = %s \n",name);
printf("size of the array is : %d",sizeof(name));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它有效,但如何?我认为一旦将内存分配给程序中的数组,就无法对其进行更改.但是,该程序的输出是:
Name = longname
Name = evenlongername
size of the array is 9
Run Code Online (Sandbox Code Playgroud)
所以编译器肯定数组的大小仍然是9.如何能够存储大小为15字节的单词'evenlongername'(包括字符串终结符)?
所以我现在正在努力学习C,并且我有一些基本的结构问题,我想清理一下:
基本上,一切都围绕着这段代码:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 127
typedef struct {
char name[MAX_NAME_LEN + 1];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
return s->name; // returns the 'name' member of a Student struct
}
/* set the name of student s
If name is too long, cut off characters after the maximum number of characters …Run Code Online (Sandbox Code Playgroud) 我知道网上有很多答案,说一个比另一个更好,但我已经明白,strncpy()在防止由于缺失而导致系统不必要的崩溃方面更好'\0'.正如许多人所说,这是非常防御性的.
我已经浏览过这个链接:为什么要使用strncpy而不是strcpy?
让我们说:
char dest[5];
char src[7] = "Hello";
strncpy(dest, src, sizeof(dest)-1);
Run Code Online (Sandbox Code Playgroud)
有了这个实现(密钥sizeof(dest)-1总是为'\0'附加提供空间),即使目标有一个截断的字符串,是不是总是更安全使用strncpy()而不是strcpy()?换句话说,我想明白了一会时,要使用strcpy()过strncpy()?
任何人都可以澄清一下吗?
char str[1];
strcpy(str, "HHHHHHHHHHHH");
Run Code Online (Sandbox Code Playgroud)
这里我声明了一个大小为1的char数组,但是程序不会崩溃,直到我输入超过12个字符而且我只有一个数组大小.为什么?