strcpy

C++笔试题汇总【转载】

痴心易碎 提交于 2019-11-26 18:36:40
转自: http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114006.html 一.找错题 试题1: void test1() {   char string [ 10 ];   char * str1 = " 0123456789 " ;  strcpy( string , str1 ); } 试题2: void test2() {   char string [ 10 ],str1[ 10 ];   int i;   for (i = 0 ; i < 10 ; i ++ )  {   str1 = ' a ' ;  }  strcpy( string , str1 ); } 试题3: void test3( char * str1) {   char string [ 10 ];   if ( strlen( str1 ) <= 10 )  {   strcpy( string , str1 );  } } 解答:   试题1字符串str1需要11个字节才能存放下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界;   对试题2,如果面试者指出字符数组str1不能在数组内结束可以给3分;如果面试者指出strcpy(string,str1

strcpy() return value

倖福魔咒の 提交于 2019-11-26 18:11:10
问题 A lot of the functions from the standard C library, especially the ones for string manipulation, and most notably strcpy(), share the following prototype: char *the_function (char *destination, ...) The return value of these functions is in fact the same as the provided destination . Why would you waste the return value for something redundant? It makes more sense for such a function to be void or return something useful. My only guess as to why this is is that it's easier and more convenient

谈谈 C++ 内存管理

对着背影说爱祢 提交于 2019-11-26 17:38:49
有多少个new就有多少个delete 二维动态数组的写法 首先开辟第一维的空间,第一维是char型的指针 char **s = new char *[ 182 ]; 在第一维的基础上,开辟第二维的空间,第二维是不定长度的char型 s[nCounts] = new char [str.length()]; 释放二维动态数组时,规则是由内到外的,先释放第二维的空间,最后再释放第一维的空间 for ( int i = 0 ; i < nCounts; ++ i) { delete[] s[i]; // delete[col] s[i]; s[i] = NULL; } delete[] s; // delete[row] s; s = NULL; s是二维指针,(声明)定义在main函数内,既然定义时在函数内,那么s指针就存放在内存中的栈区域中,使用new动态开辟的空间是在堆区域,堆区域用于存放数据,当然,每个数据都有它的内存地址,因此数据的地址是堆中的某一块地址,而栈中的s只是一个指针,这个指针指向堆区域的指定一块地址,当使用delete释放第二维的空间时,实质是回收堆上的虚拟地址块,而栈内的指针s并无发生变化 实现代码: 1 /// : handleString 2 #include <cstdlib> 3 #include < string > 4 #include <vector

C - why is strcpy() necessary

淺唱寂寞╮ 提交于 2019-11-26 17:32:14
Can someone please explain to me why strcpy() is necessary to assign strings to character arrays, such as in the following code snippet. int main(void) { char s[4]; s = "abc"; //Fails strcpy(s, "abc"); //Succeeds return 0; } What is the reason that s = "abc" fails? And why is strcpy() the only way to assign strings to char arrays after they have been declared? It seems strange to me that you have to use a function to carry out a basic assignment. Arrays in C are non-assignable and non-copy-initializable. That's just how arrays are in C. Historically, in value context (on the RHS of assignment)

Why does this intentionally incorrect use of strcpy not fail horribly?

大憨熊 提交于 2019-11-26 17:28:47
问题 Why does the below C code using strcpy work just fine for me? I tried to make it fail in two ways: 1) I tried strcpy from a string literal into allocated memory that was too small to contain it. It copied the whole thing and didn't complain. 2) I tried strcpy from an array that was not NUL -terminated. The strcpy and the printf worked just fine. I had thought that strcpy copied char s until a NUL was found, but none was present and it still stopped. Why don't these fail? Am I just getting

strcpy及strcpy_s的问题

不羁的心 提交于 2019-11-26 16:37:23
参考: C++中strcpy()函数和strcpy_s()函数的使用及注意事项 在对字符串进行拷贝赋值时 char * p = new char [ 5 ] ; strcpy ( p , "abcd" ) ; 会出现 说明编译器提醒’strcpy’方法是不安全的,原因是在字符串指针p是在堆中临时创建的空间,可能会存在内存溢出等问题,如果一定要使用该语句则需要在开头加上 # define _CRT_SECURE_NO_WARNINGS 或者使用建议使用的’strcpy_s’方法,在使用中发现 char * p = new char [ 5 ] ; strcpy_s ( p , "abcd" ) ; 会报错,原因是对于堆中创建的字符串指针,需要使用该方法的三参数版本 _ACRTIMP errno_t __cdecl strcpy_s ( _Out_writes_z_ ( _SizeInBytes ) char * _Destination , _In_ rsize_t _SizeInBytes , _In_z_ char const * _Source ) ; 即 char * p = new char [ 5 ] ; strcpy_s ( p , 5 , "abcd" ) ; 或者直接使用字符串数组 char p [ 5 ] ; strcpy_s ( p , "abcd" ) ;

Segmentation Fault in strcpy()

筅森魡賤 提交于 2019-11-26 11:37:04
问题 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. 回答1: Your typedef defines Truck as

strcpy vs strdup

风流意气都作罢 提交于 2019-11-26 09:17:51
问题 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 ? 回答1: 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

error: function returns address of local variable

╄→гoц情女王★ 提交于 2019-11-26 08:54:54
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? phoxis The local variables have a lifetime which extends only inside the block in which it is defined. The moment the control goes outside the block in which the local variable is defined, the storage

C - why is strcpy() necessary

人走茶凉 提交于 2019-11-26 05:27:52
问题 Can someone please explain to me why strcpy() is necessary to assign strings to character arrays, such as in the following code snippet. int main(void) { char s[4]; s = \"abc\"; //Fails strcpy(s, \"abc\"); //Succeeds return 0; } What is the reason that s = \"abc\" fails? And why is strcpy() the only way to assign strings to char arrays after they have been declared? It seems strange to me that you have to use a function to carry out a basic assignment. 回答1: Arrays in C are non-assignable and