strcpy

C++中的c_str()

本秂侑毒 提交于 2019-12-02 17:00:58
c_str()函数返回一个指向正规C字符串的指针 const char*,内容就是string本身 C++中的这个函数是为了与c语言兼容,c中无string类型,故必须通过string类对象的成员函数c_str()把string对象转换成c中的字符串样式 #include<iostream> #include<cstring> using namespace std; int main(){ const char* c; string s="1234"; c=s.c_str();//直接在指针上操作 cout<<c<<endl; s="abcd"; cout<<c<<endl; return 0; } 结果:1234 abcd 这样通过c指针进行操作,会是字符串发生改变。 #include<iostream> #include<cstring> using namespace std; int main(){ char *c=new char[20]; string s="1234"; strcpy(c,s.c_str());//通过strcpy()函数 头文件#include<string.h>c里面的一个字符串复制的函数 返回char* cout<<c<<endl; s="abcd"; cout<<c<<endl; return 0; } 结果:1234 1234

strcpy and strcat cause problems sometimes

允我心安 提交于 2019-12-02 14:08:42
问题 hello I have a code like the one below char *str ; strcpy(str, "\t<"); strcat(str, time); strcat(str, ">["); strcat(str, user); strcat(str, "]"); strcat(str, "("); strcat(str, baseName); strcat(str, ") $ "); printf("\String is now: %s\n", str); This code seems working but when I use XCode analyse function, it says "Function call argument is an uninitialized value" and also it sometimes causes my program crash.. when I remove it, then it works fine... Whats wrong with that? Thanks 回答1: strcpy

Is strcpy_s part of the C++ Standard? Or only part of MS Visual C++

坚强是说给别人听的谎言 提交于 2019-12-02 10:04:45
Using the function strcpy in MS Visual Studio gives me an error saying I should use strcpy_s which is safer to use. Is strcpy_s part of the C++ standard? Or is it only part of Microsoft Visual C++? Will code containing strcpy_s only compile in Visual Studio? strcpy_s() is an optional part of C11 (more formally called a "conditional feature". Implementations are permitted to not implement the "Bounds-checking interfaces" standardized in Annex K. Some other conditional features of C11 include: atomics complex types threads variable length arrays (the interesting this about this is that VLAs were

C语言-memset()

眉间皱痕 提交于 2019-12-02 09:43:06
1. memset()函数原型是extern void *memset(void *buffer, int c, int count) buffer:为指针或是数组, c:是赋给buffer的值, count:是buffer的长度. 这个函数在socket中多用于清空数组.如:原型是memset(buffer, 0, sizeof(buffer)) Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’; 例:char a[100];memset(a, ‘/0’, sizeof(a)); memset可以方便的清空一个结构类型的变量或数组。 如: struct sample_struct { char csName[16]; int iSeq; int iType; }; 对于变量: struct sample_strcut stTest; 一般情况下,清空stTest的方法: stTest.csName[0]=’/0’; stTest.iSeq=0; stTest.iType=0; 用memset就非常方便: memset(&stTest,0,sizeof(struct sample_struct)); 如果是数组: struct sample_struct TEST[10]; 则 memset(TEST,0,sizeof

C string at the end of '\0'

自古美人都是妖i 提交于 2019-12-02 09:19:08
问题 While writing c code, I tried to write strcpy code of my own, and I faced this issue. #include <stdio.h> #include <string.h> void strcpy2(char *s, char *t); int main() { char a[10] = "asds"; char b[10] = "1234567890"; strcpy2(a, b); printf("Copy completed! : %s", a); return 0; } void strcpy2(char *s, char *t) { while ((*s++ = *t++)); } Error code : Process finished with exit code -1073741819 (0xC0000005) Thanks to this question on the s.o, I learned string should ends with '\0', but why the

__strcpy_sse2_unaligned with -fno-builtin

拈花ヽ惹草 提交于 2019-12-02 07:42:24
问题 I was debugging my program, then the last line happened, how can I fix it? I used the -fno-builtin to have a look at the strcpy() but it shows that the __strcpy_sse2_unaligned is getting called. root@19:~/booksrc# gcc -fno-builtin -g char_array2.c root@19:~/booksrc# gdb -q ./a.out Reading symbols from ./a.out...done. (gdb) list 1 #include <stdio.h> 2 #include <string.h> 3 4 int main() { 5 char str_a[20]; 6 7 strcpy(str_a, "Hello World!\n"); 8 printf(str_a); 9 } (gdb) break 6 Breakpoint 1 at

Why is a string copy function just assigning the pointer not working?

淺唱寂寞╮ 提交于 2019-12-02 07:34:27
问题 Consider this implementation of strcpy : void my_strcpy(char target[], char source[]) { target = source; } int main(void) { char target[20]; char source[] = "Source String"; my_strcpy(target, source); printf("Target: %s", target); return 0; } This doesn't work and its made me question my understanding of strings and arrays in C. Here's my reasoning: target and source are really just pointers to the first elements of arrays, ie. target == &target[0] and source == &source[0] . When I set target

strcpy and strcat cause problems sometimes

可紊 提交于 2019-12-02 07:20:10
hello I have a code like the one below char *str ; strcpy(str, "\t<"); strcat(str, time); strcat(str, ">["); strcat(str, user); strcat(str, "]"); strcat(str, "("); strcat(str, baseName); strcat(str, ") $ "); printf("\String is now: %s\n", str); This code seems working but when I use XCode analyse function, it says "Function call argument is an uninitialized value" and also it sometimes causes my program crash.. when I remove it, then it works fine... Whats wrong with that? Thanks strcpy and strcat are used to copy and concatenate strings to an allocated char array. Since str in not initilized

C string at the end of '\\0'

那年仲夏 提交于 2019-12-02 06:12:24
While writing c code, I tried to write strcpy code of my own, and I faced this issue. #include <stdio.h> #include <string.h> void strcpy2(char *s, char *t); int main() { char a[10] = "asds"; char b[10] = "1234567890"; strcpy2(a, b); printf("Copy completed! : %s", a); return 0; } void strcpy2(char *s, char *t) { while ((*s++ = *t++)); } Error code : Process finished with exit code -1073741819 (0xC0000005) Thanks to this question on the s.o, I learned string should ends with '\0', but why the above code doesn't work even though it does not cause error when it is declared? (It worked well when

C++ vector容器和sort函数的学习

五迷三道 提交于 2019-12-02 04:58:53
Vector注意事项: 声明形如vector<> vec;的不是new出来而是作为普通变量的那么不需要delete, 在变量超出作用域时会自动回收 如果是用*vec = new vector<>()这种方式动态创建的vector那么需要delete vec vec里存放的元素如果不是指针那么不用delete, 这些元素在vec被释放时会被一起释放 vec里存放的元素是指针并且这些指针都是指向自己new的对象的话, 那么需要自己一个个delete c++中内存拷贝函数(C++ memcpy)详解 头文件:C++: #include<cstring> 原型: void * memcpy ( void *dest, const void *src, unsigned int count); //参数是void 功能:由src所指内存区域复制count个字节到dest所指内存区域。 说明:src和dest所指内存区域不能重叠,函数返回指向dest的指针。 函数原型: void * memcpy ( void *dest, const void *src, size_t n); 功能: 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始地址的空间内。 头文件: #include<string.h> 返回值:   函数返回一个指向dest的指针。 说明:    1