strcpy

strcpy vs. memcpy

天大地大妈咪最大 提交于 2019-11-27 09:03:22
问题 What is the difference between memcpy() and strcpy() ? I tried to find it with the help of a program but both are giving the same output. int main() { char s[5]={'s','a','\0','c','h'}; char p[5]; char t[5]; strcpy(p,s); memcpy(t,s,5); printf("sachin p is [%s], t is [%s]",p,t); return 0; } Output sachin p is [sa], t is [sa] 回答1: what could be done to see this effect Compile and run this code: void dump5(char *str); int main() { char s[5]={'s','a','\0','c','h'}; char membuff[5]; char strbuff[5]

c++ 结构

僤鯓⒐⒋嵵緔 提交于 2019-11-27 08:35:57
结构 结构是 C++ 中另一种 用户自定义的可用的数据类型 ,它 允许存储不同类型的数据项 比如,一本图书的结构: Title :标题 Author :作者 Subject :类目 Book ID :书的 ID 一、定义 struct type_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; . . } object_names; type_name 是结构体类型的名称,member_type1 member_name1 是标准的变量定义,比如 int i; 或者 float f; 或者其他有效的变量定义。在结构定义的末尾,最后一个分号之前,您可以指定一个或多个结构变量,这是可选的。 比如: struct Books { char title[50]; char author[50]; char subject[100]; int book_id; } book; 二、访问结构成员 成员访问运算符(.) #include <iostream> #include <cstring> using namespace std; // 声明一个结构体类型 Books struct Books { char title[50]; char author[50];

Difference between using strcpy() and copying the address of a the char* in C

做~自己de王妃 提交于 2019-11-27 08:17:43
问题 I have two dynamically allocated arrays. c char **a = (char**)malloc(sizeof(char*) * 5)); char **b = (char**)malloc(sizeof(char*) * 5)); for (int i = 0; i < 7, i++) { a[i] = (char*)malloc(sizeof(char)*7); b[i] = (char*)malloc(sizeof(char)*7); } If a[0] was "hello\0" and I wanted to copy a[0] to b[0] , would strcpy and pointer assignment be the same thing? For example: strcpy(b[0], a[0]) b[0] = a[0] Would these both do the same exact thing? 回答1: NO . Both are not same. In this case, strcpy(b[0

C/C++混淆点-strcat和strcpy区别

烈酒焚心 提交于 2019-11-27 05:41:34
一、原因分析 假设: char * str=NULL; str=new char[11];你想为字符串str开辟一个存储十个字符的内存空间,然后你现在有两个字符串:char * c1="abc"和char * c2="123";你想将这两个字符串都拼接在str字符串中,你想用strcat这个函数。但是你 直接用strcat编译器会报错 !!!不能这样做, 应该在拼接之前使用memset(字符串指针,0,想要设置的个数)函数对新开辟的内存空间进行清空 ,也是用0来代替。如果不这样的话, 万一新开辟的内存空间有其它值的话,strcat就不是从第一个位置开始拼接 。对应本例的情况,应该在使用strcat之前调用memset(str,0,7);用7是有效字符个数为6,还有一个隐藏字符'\0'。 当然,还有一个方法,那就是先使用strcpy(str,c1),再使用strcat(str,c2)。为什么呢?因为 strcpy把c1拷贝至str的内存空间的时候是从 第一位 开始拷贝的 ,再用strcat拼接拷贝后的字符串就可以了。 二、msmset()函数介绍 memset函数详细说明:void *memset(void *s,int c,size_t n) 总的作用:将已开辟内存空间s的首n个字节的值设为值c。也就是将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值

Segmentation Fault in strcpy()

泄露秘密 提交于 2019-11-27 05:32:14
I have a basic structure like this typedef struct struck { char* id; char* mat; int value; char* place; } *Truck; And afunction like this which creates a new "instance" of that struct: Truck CTruck(char* id, char* mat, int value, char* place) { Truck nT = (Truck) malloc(sizeof (Truck)); nT->value = value; strcpy(nT->id, id); strcpy(nT->mat, mat); strcpy(nT->place, place); return nT; } I'm getting an error in the first strcpy . It compiles without problem. Your typedef defines Truck as a struct struck * , i.e. a pointer. So it's size will be 4 or 8 depending on the architecture and not the size

Does C have a string type? [closed]

爱⌒轻易说出口 提交于 2019-11-27 04:03:12
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have recently started programming in C, coming from Java and Python. Now, in my book I have noticed that to make a "Hello World" program, the syntax is something like this: char message[10] strcpy(message,

String类的实现

大兔子大兔子 提交于 2019-11-27 01:30:34
1、在类中可以访问private成员包括两层含义:可以访问this指针的private成员;可以访问同类对象的private成员。 2、这里的String可以认为是个资源管理类,内部有个char指针,在堆上分配对象,而且对于String应该是深复制,不是复制指针,而是要复制指针指向的内容。 String类的定义(头文件): 1 #ifndef String_H_ 2 #define String_H_ 3 class String 4 { 5 public : 6 String( const char * src); 7 String( const String& rhs); 8 String& operator =( const String& rhs); 9 ~ String(); 10 private : 11 char * _data; 12 }; 13 #endif String类的实现(cpp文件): 1 #include " String.h " 2 #include <iostream> 3 String::String( const char * src) 4 { 5 if (src== NULL) 6 { 7 _data = new char [ 1 ]; 8 _data[ 0 ]= ' \0 ' ; 9 } 10 else 11 { 12 _data =

理解 strcpy方法

删除回忆录丶 提交于 2019-11-27 01:30:33
1 char * strcpy( char * strDest, const char * strSrc) 2 { 3 assert((strDest!=NULL) && (strSrc !=NULL)); // strDest和strSrc不为空 4 char * address = strDest; // 保存strDest,用于返回 5 while ( (*(strDest++) = *(strSrc++)) != ' \0 ' ); // 逐个字符赋值 6 return address; // 返回地址 7 } 注意: 1、栈上的char数组,数组名就是一个指针,这个指针的指向不能修改,也就是不能加加减减或者赋值,可以通过[]访问数组元素。 2、文本字符串赋值给char指针,这个指针指向的内容不能修改。 3、调用strcpy方法的时候,要保证:   a、保证两块内存没有重合的地方;   b、strDest最好有刚刚好的内存,可以容纳strSrc的内容。如果strDest内存大了,造成浪费。如果strDest内存小了,溢出,占到其他的内存,导致未定义行为。 转载于:https://www.cnblogs.com/nzbbody/p/3565176.html 来源: https://blog.csdn.net/weixin_30542079/article/details

Proper way to empty a C-String

狂风中的少年 提交于 2019-11-27 00:26:43
问题 I've been working on a project in C that requires me to mess around with strings a lot. Normally, I do program in C++, so this is a bit different than just saying string.empty(). I'm wondering what would be the proper way to empty a string in C. Would this be it? buffer[80] = "Hello World!\n"; // ... strcpy(buffer, ""); 回答1: It depends on what you mean by "empty". If you just want a zero-length string, then your example will work. This will also work: buffer[0] = '\0'; If you want to zero the

strcpy vs strdup

六月ゝ 毕业季﹏ 提交于 2019-11-26 23:54:50
I read that strcpy is for copying a string, and strdup returns a pointer to a new string to duplicate the string. Could you please explain what cases do you prefer to use strcpy and what cases do you prefer to use strdup ? Abdul Muheedh strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++) where as strdup is equivalent to ptr2 = malloc(strlen(ptr1)+1); strcpy(ptr2,ptr1); ( memcpy version might be more efficient) So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough. The functions strcpy