#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main ()
{
char *imsi;
unsigned int i;
int val;
char *dest;
imsi = "405750111";
strncpy(dest,imsi,5);
printf("%s",dest);
/* i = 10; */
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,i = 10赋值如上所述,代码工作正常,没有错误.当包含赋值用于编译时,错误(分段错误)发生在strncpy(dest,imsi,5); .
通过避免对变量i的优化(即,volatile int i;),即使包括赋值(i = 10),也清除错误.
考虑以下代码:
const char foo[] = "lorem ipsum"; // foo is an array of 12 characters
const auto length = strlen(foo); // length is 11
string bar(length, '\0'); // bar was constructed with string(11, '\0')
strncpy(data(bar), foo, length);
cout << data(bar) << endl;
Run Code Online (Sandbox Code Playgroud)
我的理解是strings始终分配有一个隐藏的null元素。如果是这种情况,那么bar实际上会分配12个字符,其中第12 个字符是隐藏的'\0',这是绝对安全的...如果我错了,那cout将导致未定义行为,因为没有空终止符。
有人可以帮我确认吗?这合法吗?
关于为什么要使用strncpy而不是仅使用string(const char*, const size_t)构造函数存在很多问题。我的目的是使玩具代码接近包含的实际代码vsnprintf。不幸的是,即使在这里获得了出色的答案之后,我仍然发现它的vsnprintf行为与并不相同strncpy,并且我在这里提出了一个后续问题:为什么vsnprintf不会写与strncpy相同数量的字符?
我对strncpy有困难.我试图将一个包含8个字符的字符串分成两个字符串(一个子字符串中的前6个字符,然后是另一个字符串中的剩余2个字符).为了说明特殊的困难,我将代码简化为以下内容:
include stdio.h
include stdlib.h
include string.h
define MAXSIZE 100
struct word {
char string[8];
char sub1[2];
char sub2[6];
};
typedef struct word Word;
int main(void)
{
Word* p;
p=(Word*)malloc(MAXSIZE*sizeof(Word));
if (p==NULL) {
fprintf(stderr,"not enough memory");
return 0;
}
printf("Enter an 8-character string: \n");
scanf("%s",p->string);
strncpy(p->sub2,p->string,6);
strncpy(p->sub1,p->string,2);
printf("string=%s\n",p->string);
printf("sub1=%s\n",p->sub1);
printf("sub2=%s\n",p->sub2);
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
提示用户输入.假设他们输入"12345678".那么程序的输出是:
string=1234567812123456
sub1=12123456
sub2=123456
Run Code Online (Sandbox Code Playgroud)
我期待的输出如下:
string=12345678
sub1=12
sub2=123456
Run Code Online (Sandbox Code Playgroud)
我不明白strncpy似乎是如何将数字附加到字符串上...显然我不太了解strncpy,但有人可以向我解释发生了什么吗?
为什么这个strncpy()实现在第二次运行时崩溃,而第一次运行正常?
函数strncpy
从字符串复制
n字符将源的第一个字符复制到目标.如果在n复制字符之前找到源C字符串的末尾(由空字符表示),则使用零填充目标,直到n已向其写入总计字符.如果source长于
n(因此,在这种情况下,destination可能不是空终止的C字符串),则在目标的末尾不会隐式附加空字符.
char *strncpy(char *src, char *destStr, int n)
{
char *save = destStr; //backing up the pointer to the first destStr char
char *strToCopy = src; //keeps [src] unmodified
while (n > 0)
{
//if [n] > [strToCopy] length (reaches [strToCopy] end),
//adds n null-teminations to [destStr]
if (strToCopy = '\0')
for (; n > 0 ; ++destStr)
*destStr = '\0';
*destStr = *strToCopy;
strToCopy++;
destStr++;
n--;
//stops copying …Run Code Online (Sandbox Code Playgroud) 我正试图在C中替换' '(空格)'___'(三重下划线).
这是我的代码:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *a = "12 34 56";
int a_l = strlen(a);
printf("str1: \"%s\" (%d)\n", a, a_l);
char *b = "___";
int b_l = strlen(b);
printf("str2: \"%s\" (%d)\n", b, b_l);
for (int i = 0; i < a_l; i++) {
if (a[i] == ' ') {
char *o = malloc(a_l + b_l);
strncpy(o, a, i);
strncpy(o + i, b, a_l);
//strncpy help
printf("out: \"%s\"\n", o);
} …Run Code Online (Sandbox Code Playgroud) 我是一名新的程序员,现在我已经开始使用c了.我正在尝试解码IDEv3 mp3标签,我遇到了各种各样的问题.当我使用fread()和strncpy()命令时,我注意到两者都需要将\n字符作为结束参考点.(也许我错了,这只是观察)
当我打印输出时,它们产生一个不可读的字符.作为解决问题的解决方案我使用fread()4字节而不是3字节来生成(8)\n字符(整个字节),第二步我使用strncpy()和3字节分配给分配然后我用于打印的记忆.理论上,当我使用fread()时,我不应该遇到这个问题.
代码示例:
#include <stdio.h>
#include <stdlib.h>
typedef struct{
unsigned char header_id[3]; /* Unsigned character 3 Bytes (24 bits) */
}mp3_Header;
int main (int argc, char *argv[]) {
mp3_Header first;
unsigned char memory[4];
FILE *file = fopen( name.mp3 , "rb" );
if ( (size_t) fread( (void *) memory , (size_t) 4 , (size_t) 1 , (FILE *) file) !=1 ) {
printf("Could not read the file\n");
exit (0);
} /* End of if condition */
strncpy( (char *) first.header_id …Run Code Online (Sandbox Code Playgroud) 我最初使用Microsoft/visual c ++编译器编写了一个作业,但我的课程要求我使用G ++在unix上运行和编译
但是,strncpy_s不能使用g ++编译,我的特定程序的任何解决方法?
这是使用它的代码段:
void PhoneNumber::setName(const char name[])
{
strncpy_s(_name, name, MAX_NAME_LENGTH);
}
Run Code Online (Sandbox Code Playgroud) 为什么......
char *dst = (char*) malloc(sizeof(char) * 11);
char *src = "ABCDEFGHIJKLMNOPQRSTUVQXYZ";
strncpy(dst, src, 10);
Run Code Online (Sandbox Code Playgroud)
......工作正常,但......
char *dst = "ABCDEFGHIJ\0";
char *src = "ABCDEFGHIJKLMNOPQRSTUVQXYZ\0";
strncpy(dst, src, 10);
Run Code Online (Sandbox Code Playgroud)
... 甚至 ...
char *dst = "ABCDEFGHIJ\0";
char *src = "KLMNOPQRST\0";
strncpy(dst, src, 10);
Run Code Online (Sandbox Code Playgroud)
给段错?
另外,这是怎么回事:
char *dst = (char*) malloc(sizeof(char) * 10); // also works with 9
char *src = "ABCDEFGHIJKLMNOPQRSTUVQXYZ\0";
strncpy(dst, src, 10);
Run Code Online (Sandbox Code Playgroud)
将11个字节复制到分配有10个字节的指针中原则上应该失败?
我试着编写一个程序,它从字符串s1复制第一个k字符,然后将结果与字符串s2的其余部分连接,从位置i开始,然后在结果中连接s1的其余部分.但我遇到了一些问题strncpy.它在控制台中显示了一些奇怪的字符,如@.
这是我的代码:
char* strninsert(char *s1, char *s2, int k,int i)
{
int n=strlen(s1)+strlen(s2)+10; // if i put for exemple 1000 it works
char *result=(char*)malloc(n*sizeof(char));
result[0]='\0';
strncpy(result,s1,k);
strcat(result,(s2+i));
strcat(result,(s1+k));
puts(result);
return(result);
}
int main()
{
int lens;
printf("size string 1 ");
scanf("%d",&lens);
char *s=(char*)malloc((lens+1)*sizeof(char));
getchar();
fgets(s,lens+1,stdin);
int lenx;
printf("enter size string 2 ");
getchar();
scanf("%d",&lenx);
char *x=(char*)malloc((lenx+1)*sizeof(char));
getchar();
fgets(x,lenx+1,stdin);
int lentemp=lenx+lens;
char *temp=(char*)malloc((lentemp+1)*sizeof(char));
temp=strninsert(s,x,2,3);
puts(temp);
return(0);
}
Run Code Online (Sandbox Code Playgroud)
它会在strncpy指令
之后显示奇怪的字符poes@<line.
我正在尝试使用 strstr() 在字符串中查找子字符串。仅使用 char[] 有效,char* 无效,导致分段错误。
所以这个正在工作:
int main() {
char str[] = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但这一个不起作用:
int main() {
char *str = "apple apple peach";
char *p;
p = strstr(str, "peach");
if (p!= NULL) {
strncpy(p, "apple", 5);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这个既不是:
int main() {
char *str = "apple apple peach";
char *peach = "peach";
char *p;
p = strstr(str, peach); …Run Code Online (Sandbox Code Playgroud)