strcat

How to copy or concatenate two char*

戏子无情 提交于 2019-12-06 02:41:58
How do you concatenate or copy char* together? char* totalLine; const char* line1 = "hello"; const char* line2 = "world"; strcpy(totalLine,line1); strcat(totalLine,line2); This code produces an error! segmentation fault I would guess that i would need to allocate memory to totalLine? Another question is that does the following copy memory or copy data? char* totalLine; const char* line1 = "hello"; totalLine = line1; Thanks in advance! :) I would guess that i would need to allocate memory to totalLine? Yes, you guessed correctly. totalLine is an uninitialized pointer, so those strcpy calls are

C语言strcat()函数:字符串连接(拼接)

巧了我就是萌 提交于 2019-12-05 13:54:20
C语言strcat()函数:字符串连接(拼接) C语言 strcat() 函数用来将两个字符串连接(拼接)起来。 头文件:string.h 语法/原型: char*strcat(char* strDestination, const char* strSource); 参数说明: strDestination:目的字符串; strSource:源字符串。 strcat() 函数把 strSource 所指向的字符串追加到 strDestination 所指向的字符串的结尾,所以必须要保证 strDestination 有足够的内存空间来容纳两个字符串,否则会导致溢出错误。 注意:strDestination 末尾的 \0 会被覆盖,strSource 末尾的 \0 会一起被复制过去,最终的字符串只有一个 \0 。 返回值:指向 strDestination 的指针。 【实例】使用C语言 strcat() 函数将用户输入的两个字符串拼接在一起。 #include <stdio.h> #include <string.h> int main(){   char str1[101] = { 0 };   char str2[50] = { 0 };   gets(str1);   gets(str2);   strcat(str1, str2);   puts(str1);  

c 语言 strcat函数(字符串连接函数)

我的梦境 提交于 2019-12-04 20:21:20
srtcat函数原型在c中的<string.h>中。 语法: strcat(字符串a,字符串b); 将字符串a和b连接之后,储存到a中,并返回a的地址。 *函数返回值为字符串a的地址。 *c语言似乎是没有字符串类型的数据类型,这里的字符串一般讲的是字符数组char x[ ]和字符指针char *p。 来源: https://www.cnblogs.com/flyingpenguins/p/11881481.html

C++ strcat_s

浪子不回头ぞ 提交于 2019-12-03 13:30:31
{   //如果用strcat() 连接时被连接的字符串是堆内存会 [损坏内存],使堆内存无法释放   建议用strcat_s() } 来源: https://www.cnblogs.com/YZFHKMS-X/p/11797125.html

error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.【转载】

岁酱吖の 提交于 2019-12-03 09:18:51
文章出处 https://blog.csdn.net/qq_38721302/article/details/82850292 今天编写C++程序在使用头文件#include<cstring>中的strcpy()和strcat()函数时出现了一个错误:error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.在网上搜了一下大概知道怎么解决了,并且知道为什么出现这个错误——出现这个错误时,是因为strcpy()和strcat()函数不安全造成的溢出。 解决方法是:找到【项目属性】,点击【C++】里的【预处理器】,对【预处理器】进行编辑,在里面加入一段代码:_CRT_SECURE_NO_WARNINGS。 这样就解决了。 来源: https://www.cnblogs.com/nxopen2018/p/11785218.html

Concatenate Strings in C/C++

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How do I concatenate Strings with C/C++? I tried the following ways: PS: errorInfo is a char * I should return it. errorInfo = strcat("Workflow: ", strcat( workflowToString(workflow).utf8(), strcat(" ERROR: ", errorCode.utf8))); sprintf(errorInfo, "Workflow %s ERROR: %s", workflowToString(workflow).utf8(), errorCode.utf8()); errorInfo = "Workflow: " + workflowToString(workflow).utf8() + " ERROR: " + errorCode.utf8; Just the sprintf compiles but when running my application crash. PS: I'm using NDK from Android 回答1: According to this page

C string append

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to append two strings. I used the following command: new_str = strcat(str1, str2); This command changes the value of str1 . I want new_str to be the concatanation of str1 and str2 and at the same time str1 is not to be changed. 回答1: You need to allocate new space as well. Consider this code fragment: char * new_str ; if((new_str = malloc(strlen(str1)+strlen(str2)+1)) != NULL){ new_str[0] = '\0'; // ensures the memory is an empty string strcat(new_str,str1); strcat(new_str,str2); } else { fprintf(STDERR,"malloc failed!\n"); // exit? }

C “error: function returns address of local variable”

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm beginner with C and I am learning on my own. I am creating the following function: char *foo(int x){ if(x < 0){ char a[1000]; char b = "blah"; x = x - 1; char *c = foo(x); strcpy(a, b); strcat(a, c); return a; } blah ... } I am basically trying to return an appended string, but I get the following error: "error: function returns address of local variable", any suggestions, how to fix this? 回答1: The local variables have a lifetime which extends only inside the block in which it is defined. The moment he control goes outside the block in

Why does MSVC++ consider “std::strcat” to be “unsafe”? (C++)

匿名 (未验证) 提交于 2019-12-03 01:45:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When I try to do things like this: char* prefix = "Sector_Data\\sector"; char* s_num = "0"; std::strcat(prefix, s_num); std::strcat(prefix, "\\"); and so on and so forth, I get a warning warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. Why is strcat considered unsafe, and is there a way to get rid of this warning without using strcat_s? Also, if the only way to get rid of the warning is to use strcat_s, how does it work (syntax-wise: apparently it does not take two arguments). 回答1: Because

C strcat - warning: passing arg 2 of `strcat&#039; makes pointer from integer without a cast

匿名 (未验证) 提交于 2019-12-03 01:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm having a problem with the program below. I'm trying to scan through a string command entered by the user for certain words. My major issue right now is that when I run the following I get a warning saying that "passing arg 2 of `strcat' makes pointer from integer without a cast". My intent is to loop through the first three characters of the string "s", concatenate them onto a string "firstthree", and later check the value of the string "firstthree". Any help is appreciated. #include #include #include #include #include #include #include