strcpy

c++中char *与string之间的相互转换及原理

隐身守侯 提交于 2019-12-06 21:54:16
1. string转const char* string s = "abc" ; const char * c_s = s . c_str (); 2. const char*转string 直接赋值即可 const char * c_s = "abc" ; string s ( c_s ); 3. string转char* string s = "abc" ; char * c ; const int len = s . length (); c = new char [ len + 1 ]; strcpy ( c , s . c_str ()); 4. char*转string char * c = "abc" ; string s ( c ); 5. const char*转char* const char * cpc = "abc" ; char * pc = new char [ 100 ]; //足够长 strcpy ( pc , c) 语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。 注意:一定要使用strcpy()函数 等来操作方法c

“Abort trap: 6” error in C?

雨燕双飞 提交于 2019-12-06 08:06:06
问题 I'm a beginner to C but I have this code running on xcode through gcc on terminal: #include <stdio.h> #include <string.h> int main(){ char name[12] = "Roman Mirov"; printf("My name is %s\n", name); name[8] = 'k'; printf("My name is %s\n", name); char greeting[] = "hello"; printf("%s %s\n", greeting, name); strcpy(greeting, "greetings, "); printf("%s%s\n", greeting, name); return 0; } And it outputs this: My name is Roman Mirov My name is Roman Mikov hello Roman Mikov Abort trap: 6 My question

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

format ’%s’ expects argument of type ’char *’

不打扰是莪最后的温柔 提交于 2019-12-05 23:36:23
For exercising my programming skills in C I'm trying to write the strncpy function by myself. Doing that I kept hitting errors, solving most of them eventually I'm stuck with no further inspiration to go on. The error I receive is: 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); The thing is that it's a very common error and that it's also already described on SO, only I can't seem to apply the tips other people have already pointed out. I get that I'm using a wrong type when

strcpy() and arrays of strings

蹲街弑〆低调 提交于 2019-12-05 19:12:16
I need to store the input from a user into an array of strings. #include <stdlib.h> #include <stdio.h> #include <string.h> char *history[10] = {0}; int main (void) { char input[256]; input = "input"; strcpy(history[0], input); return (EXIT_SUCCESS); } Running it on the terminal I get a Segmentation Fault and in NetBeans I get main.c:11: error: incompatible types in assignment. I also tried to shift all the history to store the newest input into the first position (history[0]). history[9] = history[8]; history[8] = history[7]; history[7] = history[6]; history[6] = history[5]; history[5] =

How to fix strcpy so that it detects overlapping strings

梦想的初衷 提交于 2019-12-05 16:48:30
问题 In an interview, I was asked to write an implementation of strcpy and then fix it so that it properly handles overlapping strings. My implementation is below and it is very naive. How do I fix it so that: It detects overlapping strings and after detecting, how do we deal with the overlap and proceed? char* my_strcpy(char *a, char *b) { if (a == NULL || b == NULL) { return NULL; } if (a > b) { //we have an overlap? return NULL; } char *n = a; while (*b != '\0') { *a = *b; a++; b++; } *a = '\0'

How does strcpy_s work?

…衆ロ難τιáo~ 提交于 2019-12-05 16:16:55
问题 As we all know, strcpy_s is a safety version of strcpy. But I wonder how it works ... let's see some examples. strpy_s's declaration: errno_t strcpy_s(_CHAR *_DEST, size_t _SIZE, const _CHAR *_SRC) eg1 char dest[5]; char* src = "abcdefg"; strcpy_s(dest,5,src); It will return an assertion. I think I can understand this, use _SIZE to make sure we can't copy more characters than _SIZE But.. I can't understand this: char dest[5]; char* src = "abcdefg"; strcpy_s(dest,10,src); we can still get a

Alternative of strcpy in c++

痞子三分冷 提交于 2019-12-05 03:00:49
In C i used strcpy to make a deep copy of a string, but is it still 'fine' to use strcpy in C++ or are there better alternatives which i should use instead ? In C++ the easiest way is usually to use the std::string class instead of char*. #include <string> ... std::string a = "Hello."; std::string b; b = a; The line "b = a;" does the same thing you would usually do with strcpy. I put this in the comment above, but just to make the code readable: std::string a = "Hello."; std::string b; b = a.c_str(); // makes an actual copy of the string b = a; // makes a copy of the pointer and increments the

Why no sanity checks in legacy strcpy()

橙三吉。 提交于 2019-12-05 01:32:56
Following is the most popular implementation of strcpy in traditional systems. Why dest and src are not checked for NULL in the start? I heard once that in old days the memory was limited so short code was always preferred. Will you implement strcpy and other similar functions with NULL pointer checks at the start now days? Why not? char *strcpy(char *dest, const char *src) { char *save = dest; while(*dest++ = *src++); return save; } NULL is a bad pointer, but so is (char*)0x1 . Should it also check for that? In my opinion (I don't know the definitive reason why), sanity checks in such a low

“Abort trap: 6” error in C?

荒凉一梦 提交于 2019-12-04 13:05:33
I'm a beginner to C but I have this code running on xcode through gcc on terminal: #include <stdio.h> #include <string.h> int main(){ char name[12] = "Roman Mirov"; printf("My name is %s\n", name); name[8] = 'k'; printf("My name is %s\n", name); char greeting[] = "hello"; printf("%s %s\n", greeting, name); strcpy(greeting, "greetings, "); printf("%s%s\n", greeting, name); return 0; } And it outputs this: My name is Roman Mirov My name is Roman Mikov hello Roman Mikov Abort trap: 6 My question exactly is, why it generates error instead of showing the last line as output "greetings, Roman Mikov"