strcpy

C strcpy() - evil?

余生长醉 提交于 2019-11-29 20:12:17
Some people seem to think that C's strcpy() function is bad or evil. While I admit that it's usually better to use strncpy() in order to avoid buffer overflows, the following (an implementation of the strdup() function for those not lucky enough to have it) safely uses strcpy() and should never overflow: char *strdup(const char *s1) { char *s2 = malloc(strlen(s1)+1); if(s2 == NULL) { return NULL; } strcpy(s2, s1); return s2; } *s2 is guaranteed to have enough space to store *s1 , and using strcpy() saves us from having to store the strlen() result in another function to use later as the

C implementention of strcpy does not change variable value [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-29 18:25:37
This question already has an answer here: Changing address contained by pointer using function 5 answers So, i am trying to implement my own strcpy to have a better understanding of how pointers work in C, and so far i am at a loss. I tried a lot of different approaches, tried using code from the net to maybe base mine on later, but even those wont work. I believe i might be calling the function in the wrong way on the main method. I am passing a pointer of the variable to the function, so that the function can change the value of the variable and on the function call, i am giving the adress

C语言宏定义中的#,##,#@及\符号的作用

我的梦境 提交于 2019-11-29 10:06:45
1、# (stringizing)字符串化操作符 作用:将宏定义中的传入 参数名 转换成用一对双引号括起来参数名字符串。其只能用于有传入参数的宏定义中,且必须置于宏定义体中的参数名前。 如: #define example( instr ) printf( "the input string is:\t%s\n", #instr ) #define example1( instr ) #instr 当使用该宏定义时: example( abc ); // 在编译时将会展开成:printf("the input string is:\t%s\n","abc"); string str = example1( abc ); // 将会展成:string str="abc"; 注意, 对空格的处理: a、忽略传入参数名前面和后面的空格。 如:str=example1( abc ); 将会被扩展成 str="abc"; b、当传入参数名间存在空格时,编译器将会自动连接各个子字符串,用每个子字符串之间以一个空格连接,忽略剩余空格。 如:str=exapme( abc def); 将会被扩展成 str="abc def"; 2、## (token-pasting)符号连接操作符 作用:将宏定义的多个形参转换成一个实际 参数名 。 如: #define exampleNum( n ) num#

C语言指针(一)

若如初见. 提交于 2019-11-29 08:25:19
指针与地址 一元运算符&可用于取一个对象的地址,地址运算符&只能应用于内存中的对象,即变量与数组元素。不能作用于表达式、常量或者register类型的变量。 p = &c; 一元运算符* 是 间接寻址 或 间接引用运算符 。当它作用于指针时,将访问指针所指向的对象,下面代码段说明了程序中如何使用&和*: int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[0]; /* ip now points to z[0] */ int * ip ,该声明表明表达式*ip的结果是int类型。 指针只能指向某种特定类型的对象(一个类外是指向void类型指针可以存放指向任何指针,但他不能间接引用自身) 一元运算符*和&的优先级比算术运算符的优先级高。但是下面表达式中圆括号是必须的,否则,该表达式将对ip进行加1运算,这是因为,类似于 *和++这样的一元运算符遵循从右至左的结合顺序。 (*ip)++ 指针也是变量,所以程序中可以直接使用,不用通过间接引用的方法使用。若iq是另一个指向整型的指针,那么下式表示将ip的中的值拷贝到iq中

'strcpy' with 'malloc'?

三世轮回 提交于 2019-11-29 06:43:25
问题 Is it safe to do something like the following? #include <stdio.h> #include <malloc.h> #include <string.h> int main(void) { char* msg; strcpy(msg, "Hello World!!!"); //<--------- printf("%s\n", msg); return 0; } Or should the following be used? char* msg = (char*)malloc(sizeof(char) * 15); 回答1: Your original code does not assign msg. Attempting to strcpy to it would be bad. You need to allocate some space before you strcpy into it. You could use malloc as you suggest or allocate space on the

segmentation fault with strcpy [duplicate]

我的梦境 提交于 2019-11-29 02:49:00
问题 This question already has answers here : Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer (5 answers) Closed 3 years ago . I am wondering why am I getting segmentation fault in the below code. int main(void) { char str[100]="My name is Vutukuri"; char *str_old,*str_new; str_old=str; strcpy(str_new,str_old); puts(str_new); return 0; } 回答1: You haven't initialized *str_new so it is just copying str_old to some random address. You need to do either this:

strcpy vs. memcpy

北战南征 提交于 2019-11-28 15:12:28
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] 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]; memset(membuff, 0, 5); // init both buffers to nulls memset(strbuff, 0, 5); strcpy(strbuff,s); memcpy

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

蹲街弑〆低调 提交于 2019-11-28 14:12:33
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? haccks NO . Both are not same. In this case, strcpy(b[0], a[0]) is correct way to copy the string pointed by a[0] to b[0] . In case of b[0] = a[0] , memory

C implementention of strcpy does not change variable value [duplicate]

我们两清 提交于 2019-11-28 13:02:13
问题 This question already has an answer here: Changing address contained by pointer using function 5 answers So, i am trying to implement my own strcpy to have a better understanding of how pointers work in C, and so far i am at a loss. I tried a lot of different approaches, tried using code from the net to maybe base mine on later, but even those wont work. I believe i might be calling the function in the wrong way on the main method. I am passing a pointer of the variable to the function, so

Access violation when using strcpy?

╄→гoц情女王★ 提交于 2019-11-28 11:44:29
I've tried reinventing the strcpy C function, but when I try to run it I get this error: Unhandled exception at 0x00411506 in brainf%ck.exe: 0xC0000005: Access violation writing location 0x00415760. The error occurs in the *dest = *src; line. Here's the code: char* strcpy(char* dest, const char* src) { char* dest2 = dest; while (*src) { *dest = *src; src++; dest++; } *dest = '\0'; return dest2; } EDIT: Wow, that was fast. Here's the calling code (strcpy is defined in mystring.c): #include "mystring.h" #include <stdio.h> int main() { char* s = "hello"; char* t = "abc"; printf("%s", strcpy(s, t)