strcpy

How to fix strcpy so that it detects overlapping strings

…衆ロ難τιáo~ 提交于 2019-12-04 02:25:29
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'; return n; } int main(int argc, char *argv[]) { char str1[] = "wazzupdude"; char *after_cpy = my

CString, BSTR, LPCTSTR 概念

纵饮孤独 提交于 2019-12-03 13:21:59
CString是一个动态TCHAR数组 , BSTR是一种专有格式的字符串(需要用系统提供的函数来操纵 ) LPCTSTR只是一个常量的TCHAR指针。 CString 是一个完全独立的类,动态的TCHAR数组,封装了+等操作符和字符串操作方法。 typedef OLECHAR FAR* BSTR; typedef const char * LPCTSTR; vc++中各种字符串的表示法 首先char* 是指向ANSI字符数组的指针,其中每个字符占据8位(有效数据是除掉最高位的其他7位),这里保持了与传统的C,C++的兼容。 LP的含义是长指针(long pointer)。LPSTR是一个指向以‘\0’结尾的ANSI字符数组的指针,与char*可以互换使用,在win32中较多地使用LPSTR。而LPCSTR中增加的‘C’的含义是“CONSTANT”(常量),表明这种数据类型的实例不能被使用它的API函数改变,除此之外,它与LPSTR是等同的。 1. LP表示长指针,在win16下有长指针(LP)和短指针(P)的区别,而在win32下是没有区别的,都是32位.所以这里的LP和P是等价的. 2. C表示const 3. T是TCHAR,在采用Unicode方式编译时是wchar_t,在普通时编译成char. 为了满足程序代码国际化的需要,业界推出了Unicode标准

Why must a pointer to a char array need strcpy to assign characters to its array and double quotes assignment will not work?

断了今生、忘了曾经 提交于 2019-12-03 12:10:06
The first example does not work when you go to delete the pointer. The program either hangs when I add the null terminator or without it I get: Debug Assertion Failed Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) from Visual Studio 2008 //Won't work when deleting pointer: char *at = new char [3]; at = "tw"; // <-- not sure what's going on here that strcpy does differently at[2] = '\0'; // <-- causes program to hang delete at; //Works fine when deleting pointer: char *at = new char [3]; strcpy(at,"t"); at[1] = 'w'; at[2] = '\0'; delete at; So what's going on when I use double quotes

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

字符串复制

帅比萌擦擦* 提交于 2019-12-03 04:08:39
功能要求:把src 所指由NULL 结束的字符串复制到dest 所指的数组中。 分析:如果编写一个标准strcpy 函数的总分值为10,下面给出几个不同得分的答案: //得2 分 void strcpy( char strDest, char strSrc ) { while( ( strDest++ = strSrc++) != '/0' ); } //得4 分 void strcpy( char strDest, const char strSrc ) { //将源字符串加const,表明其为输入参数,加2 分 while( ( strDest++ = strSrc++) != '/0' ); } //得7 分 void strcpy(char strDest, const char strSrc) { //对源地址和目的地址加非0 断言,加3 分 assert( (strDest != NULL) && (strSrc != NULL) ); while( ( strDest++ = strSrc++) != '/0' ); } //得9 分 //为了实现链式操作,将目的地址返回,加2 分! char * strcpy( char strDest, const char strSrc ) { assert( (strDest != NULL) && (strSrc !=

Error:incompatible types in assignment of &#039;const char[5]&#039; to &#039;char[10]&#039;

匿名 (未验证) 提交于 2019-12-03 02:54:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have defined c as char c[][10] in function definition and used it like c[i]="gray"; Whats wrong? I searched on net, it shows the same syntax. Thanks. 回答1: You cannot use assignment ( = ) on an array. If you change c to an array of pointers, that might work, depending on what you need to do with it. const char *c[20]; c[i] = "gray"; Or if the declared type must be array of arrays, you could use strncpy : char c[20][10]; strncpy(c[i], "gray", sizeof(c[i])); 回答2: The problem is that arrays are not assignable in C. String constants like "gray"

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

匿名 (未验证) 提交于 2019-12-03 02:50:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When writing a project, I ran into a strange issue. This is the minimal code I managed to write to recreate the issue. I am intentionally storing an actual string in the place of something else, with enough space allocated. // #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdint.h> #include <stddef.h> // For offsetof() typedef struct _pack{ // The type of `c` doesn't matter as long as it's inside of a struct. int64_t c; } pack; int main(){ pack *p; char str[9] = "aaaaaaaa"; // Input size_t len = offsetof(pack, c) +

How to Bypass a Standard C++ Function While Maintaining Its Functionality

匿名 (未验证) 提交于 2019-12-03 02:33:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am looking for a way to be able to redefine a set of POSIX functions but then end the redefinition with a call to the original function. The idea is that I am trying to create a layer that can restrict what OS API's can be called depending on which "profile" is active. This "profile" determines what set of functions are allowed and any not specified should not be used. For example, if in one profile I am not allowed to use strcpy, I would like to be able to either cause a compile time error (via static_assert) or print something to the

Can't step into string.h function with GDB

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Having trouble stepping into string.h in GDB 7.5. Here's a simple example program: Source code: #include #include int main () { char str1 [ 20 ]; strcpy ( str1 , "STEP INTO ME\n" ); printf ( str1 ); } Compiled: ~$ gcc -g foo.c Invoked: ~$ gdb -q ./a.out GDB: ( gdb ) break 5 Breakpoint 1 at 0x8048471 : file foo . c , line 6. ( gdb ) break strcpy Function "strcpy" not defined . Make breakpoint pending on future shared library load ? ( y or [ n ]) y Breakpoint 2 ( strcpy ) pending . ( gdb ) run Starting program : /home/ user / a . out

C strtok() split string into tokens but keep old data unaltered

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have the following code: #include #include int main (void) { char str[] = "John|Doe|Melbourne|6270|AU"; char fname[32], lname[32], city[32], zip[32], country[32]; char *oldstr = str; strcpy(fname, strtok(str, "|")); strcpy(lname, strtok(NULL, "|")); strcpy(city, strtok(NULL, "|")); strcpy(zip, strtok(NULL, "|")); strcpy(country, strtok(NULL, "|")); printf("Firstname: %s\n", fname); printf("Lastname: %s\n", lname); printf("City: %s\n", city); printf("Zip: %s\n", zip); printf("Country: %s\n", country); printf("STR: %s\n", str); printf(