标签: strncpy

strncpy并不总是空终止

我使用下面的代码:

char filename[ 255 ];
strncpy( filename, getenv( "HOME" ), 235 );
strncat( filename, "/.config/stationlist.xml", 255 );
Run Code Online (Sandbox Code Playgroud)

收到此消息:

(warning) Dangerous usage of strncat - 3rd parameter is the maximum number of characters to append.
(error) Dangerous usage of 'filename' (strncpy doesn't always null-terminate it).
Run Code Online (Sandbox Code Playgroud)

c strncpy

4
推荐指数
2
解决办法
7577
查看次数

更改静态数组

我在文件中声明了一个静态变量:

static char *msgToUser[] = {
    "MSG1                ", 
    "MSG2                ",
};
Run Code Online (Sandbox Code Playgroud)

在我正在做的一个类的方法之一:

void InfoUser::ModifyMsg( BYTE msgIdx, char *msgString ){
    strncpy( msgToUser[ idx ], msgString, DISPLAY_SIZE );
}
Run Code Online (Sandbox Code Playgroud)

当我执行strncopy时,程序崩溃了.我不确定我做错了什么

c linux arrays segmentation-fault strncpy

3
推荐指数
1
解决办法
1518
查看次数

strncpy会覆盖现有的字符串

我创建了一个将数字转换为罗马数字的函数.我知道转换本身的逻辑是正确的,但是,每次调用strncpy时,它都会覆盖之前的"rom"值.我甚至试着背靠背调用它只返回后者.

这是代码中的一个片段:

   char* rom = (char*) calloc (10,sizeof(char));

    while(intval>=1000){
        intval -= 1000;
    strncpy(rom,"M",2);
    }
Run Code Online (Sandbox Code Playgroud)

也许使用calloc是问题的一部分,但我尝试使用malloc,它给了我相同的结果.

c strncpy

3
推荐指数
1
解决办法
7313
查看次数

C 中将字符从字符串复制到另一个字符串

我有一个字符串 AAbbCC 我需要的是复制前两个并将它们添加到数组中,然后复制中间两个并将它们添加到数组中,最后复制最后两个并将它们添加到数组中。

这就是我所做的:

char color1[2];
char color2[2];
char color3[2];

strncpy(color1, string, 2); // I take the first two characters and put them into color1

// now I truncate the string to remove those AA values:

string = strtok(string, &color1[1]);

// and when using the same code again the result in color2 is bbAA:

strncpy(color2, string, 2); 
Run Code Online (Sandbox Code Playgroud)

它通过了那些 bb,但也通过了前一个的 AA ..即使数组只有两个位置,当我在其上使用 strtol 时,它给了我一些大值,而不是我正在寻找的 187 ..如何摆脱它?或者如何让它以其他方式工作?任何意见,将不胜感激。

c string truncate char strncpy

3
推荐指数
1
解决办法
3万
查看次数

C++将const char*复制到char*

我有一个功能

ClassA::FuncA(const char *filePath)
Run Code Online (Sandbox Code Playgroud)

并希望将此const char字符串*复制到char*!

我的解决方案

char *argv[2];
int length = strlen(filePath);
argv[1] = new char(length +1);
strncpy(argv[1], filePath, length); 
Run Code Online (Sandbox Code Playgroud)

在此之后,我在argv [1]中得到了所需的字符,还有其他一些未定义的字符!

文件路径:

"C:\用户\用户A \Parameter.xmlþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþîþKŸQyá•"

这里有什么不对?strlen的长度还可以!

c++ char strncpy

3
推荐指数
1
解决办法
1万
查看次数

将字符长度从数组复制到std :: string

我试图将5个字符从字符数组复制到 std::string

char name[] = "Sally Magee";
std::string first;
copy(name, name + 5, first.begin()); //from #include <algorithm>
std::cout << first.c_str();
Run Code Online (Sandbox Code Playgroud)

但是我得到了字符串加上一大堆我不想要的不可打印的字符.有任何想法吗?谢谢.

c++ arrays string copy strncpy

3
推荐指数
1
解决办法
3696
查看次数

strncpy()和memcpy()有什么不同?

strncpy()memcpy()是一样的吗?

由于strncpy()只接受char*作为参数,因此将整数数组Icast为char*.

为什么它会得到不同的输出?

这是我的代码,

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#define SIZE 5


void print_array(int *array, char *name ,int len) {
    int i;
    printf("%s ={ ",name);
    for(i=0; i<len;i++)
        printf("%d," ,array[i]);
    printf("}\n");
}

int main(void){
    char string1[SIZE] = {'1','2','3','4','\0'};
    char string2[SIZE], string3[SIZE];
    int array1[SIZE] = {1,2,3,4,5};
    int array2[SIZE],array3[SIZE];

    strncpy(string2,string1,sizeof(string1));
    memcpy(string3, string1,sizeof(string1));
    printf("string2 =%s \n",string2);
    printf("string3 =%s \n",string3);

    strncpy((char *)array2, (char*)array1 , sizeof(array1));
    memcpy(array3,array1,sizeof(array1));
    print_array(array2,"array2",SIZE);
    print_array(array3,"array3",SIZE);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

事实证明

string2 =1234
string3 =1234
array2 ={ 1,0,0,0,0,} …
Run Code Online (Sandbox Code Playgroud)

c memcpy strncpy

3
推荐指数
1
解决办法
2757
查看次数

格式'%s'需要'char*'类型的参数

为了在C中练习我的编程技巧,我试图自己编写strncpy函数.这样做我总是遇到错误,解决了大部分错误,最终我没有进一步的灵感继续下去.

我收到的错误是:

ex2-1.c:29:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
   printf("The copied string is: %s.\n", stringb);
Run Code Online (Sandbox Code Playgroud)

问题是,这是一个非常常见的错误,并且它也已经在SO上描述,但我似乎无法应用其他人已经指出的提示.我知道在打印变量时我使用了错误的类型,当我使用%d格式时,它将返回一个整数,这可能是第一个字符的ASCII值,因为它在增加最大数字时不会改变要复制的字节数.

使用GDB我发现迭代通过while循环的b变量保持正确的字符串,但我似乎无法打印它.

我可能缺乏关于C语言的非常基本的知识部分而且我为这个新手问题(再一次)提出了道歉.如果您能提供反馈或指出我的代码中的其他缺陷,我将不胜感激.

#include <stdlib.h>
#include <stdio.h>

void strmycpy(char **a, char *b, int maxbytes) {
  int i = 0;
  char x = 0;

  while(i!=maxbytes) {
  x = a[0][i];
  b[i] = x;
  i++;
  }

  b[i] = 0;

}


int main (int argc, char **argv) {
  int maxbytes = atoi(argv[2]);
  //char stringa;
  char stringb;
  if (argc!=3 || …
Run Code Online (Sandbox Code Playgroud)

c string printf strcpy strncpy

3
推荐指数
1
解决办法
8566
查看次数

cppcheck-terminateStrncpy

cppcheck 新手。无法弄清楚如何解决此问题(cppcheck 警告)。任何帮助,将不胜感激。

 if (!call_initialized)
 { char id1[16];
   char id1[16];
   char* dummy_char_ptr = inet_ntoa(*((in_addr*)&source_ip));
   std::strncpy(id1, dummy_char_ptr, 16);
   dummy_char_ptr=inet_ntoa(*((in_addr*)&destination_ip));
   std::strncpy(id2, dummy_char_ptr, 16);
   dummy_char_ptr=NULL;
   std::cerr << id1 << " -----> " << id2 << std::endl;
   return 0;
   }
Run Code Online (Sandbox Code Playgroud)

错误(警告) - 调用 strncpy() 后,缓冲区“id2”可能不会以零结尾。

c++ strncpy static-code-analysis cppcheck

3
推荐指数
1
解决办法
314
查看次数

gcc-8 Wstringop 截断

我正在尝试修复一些 gcc-8 抱怨 Wstringop-truncation 的 C 代码(代码在这里

\n\n

当在我无法控制的服务器上编译该代码时,既不能添加编译指示语句,也不能禁用 Wstringop-truncation 诊断,我收到的警告是:

\n\n
gcc-8  -I"/home/hornik/tmp/R/include" -DNDEBUG -I./cqdb/include -I./crf/src -I./liblbfgs/include -I./include -I"/home/hornik/lib/R/Library/3.6/x86_64-linux-gnu/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -Wall -pedantic -mtune=native -c cqdb/src/cqdb.c -o cqdb/src/cqdb.o\ncqdb/src/cqdb.c: In function \xe2\x80\x98cqdb_writer_close\xe2\x80\x99:\ncqdb/src/cqdb.c:270:5: warning: \xe2\x80\x98strncpy\xe2\x80\x99 output truncated before terminating nul copying 4 bytes from a string of the same length [-Wstringop-truncation]\n     strncpy((char*)header.chunkid, CHUNKID, 4);\n     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\ncqdb/src/cqdb.c: In function \xe2\x80\x98cqdb_reader\xe2\x80\x99:\ncqdb/src/cqdb.c:469:9: warning: \xe2\x80\x98strncpy\xe2\x80\x99 specified bound 4 equals destination size [-Wstringop-truncation]\n         strncpy((char*)db->header.chunkid, (const char*)p, 4);\n         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想重写 strncpy 语句以删除这些警告。我是否正确,我需要替换以下几行

\n\n …

c gcc strncpy gcc8

3
推荐指数
1
解决办法
7628
查看次数