[英]How to copy char *str to char c[] in C?
Trying to copy a char *str
to char c[]
but getting segmentation fault or invalid initializer error.尝试将
char *str
复制到char c[]
,但出现分段错误或初始化程序无效错误。
Why is this code is giving me a seg fault ?为什么这段代码给我一个段错误?
char *token = "some random string";
char c[80];
strcpy( c, token);
strncpy(c, token, sizeof c - 1);
c[79] = '\0';
char *broken = strtok(c, "#");
use strncpy()
rather than strcpy()
使用
strncpy()
而不是strcpy()
/* code not tested */
#include <string.h>
int main(void) {
char *src = "gkjsdh fkdshfkjsdhfksdjghf ewi7tr weigrfdhf gsdjfsd jfgsdjf gsdjfgwe";
char dst[10]; /* not enough for all of src */
strcpy(dst, src); /* BANG!!! */
strncpy(dst, src, sizeof dst - 1); /* OK ... but `dst` needs to be NUL terminated */
dst[9] = '\0';
return 0;
}
char *str = "Hello";
char c[6];
strcpy( c, str );
use strncpy to be sure to not copy more charachters than the char[] can contains 使用strncpy以确保不会复制比char []可以包含的更多的字符串
char *s = "abcdef";
char c[6];
strncpy(c, s, sizeof(c)-1);
// strncpy is not adding a \0 at the end of the string after copying it so you need to add it by yourself
c[sizeof(c)-1] = '\0';
Edit: Code added to question 编辑:代码添加到问题
Viewing your code the segmentation fault could be by the line 查看代码可能会出现分段错误
strcpy(c, token)
The problem is if token length is bigger than c length then memory is filled out of the c var and that cause troubles. 问题是如果令牌长度大于c长度,则从c var中填充内存并导致麻烦。
char c[] must have some size; char c []必须有一些大小;
for example 例如
char c[]= "example init string";
// that set up table c to c[19]; //将表c设置为c [19]; You can allocate it directly at the begginign of Your program;
您可以直接在您的程序的开头分配它;
char c[19] = {0}; // null filled table
char c[i] is the pointer so You don't need to copy anything; char c [i]是指针,所以你不需要复制任何东西; char c[19] ;
char c [19]; c = "example init string";
c =“example init string”; // now &c[0] points the same address;
// now&c [0]指向同一地址;
Copy can be done wiht 复制可以完成
strcpy(dst, src);
but MS force You to use secure function: 但MS强制您使用安全功能:
strcpy_s(dst,buffsize,src);
Edited: Thanks for adding the code. 编辑:感谢您添加代码。
Perhaps the segfault occurs here: 也许这里出现了段错误:
strncpy(c, token, sizeof c - 1);
sizeof has a the same precedence as '-' from right to left, so it is probably processed as : sizeof从右到左具有与' - '相同的优先级,因此它可能被处理为:
strncpy(c, token, sizeof( c - 1 ) );
instead of 代替
strncpy(c, token, sizeof(c) - 1);
which is probably what you wanted 这可能是你想要的
(reference: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence ) (参考: http : //en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence )
It's been a while since i coded in c/c++, but c[80] is probably allocated on the stack. 我用c / c ++编写了一段时间,但c [80]可能是在堆栈上分配的。 If you use char *c and strdup or similiar you get it allocated on the heap where strtok can access it.
如果你使用char * c和strdup或者类似的,你可以在strtok可以访问它的堆上分配它。
Try something like this. 尝试这样的事情。
char *token = "some random string";
char *c;
c = strdup(token);
char *broken = strtok(c, "#");
free(c);
char text[] = "Some sample text";
char* str = &text;
// To create a copy of string into another string of same size from pointer array.
char temp[strlen(str)+1];
strcpy(temp,str);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.