strcpy

strcpy和memcpy的区别(转)

你。 提交于 2019-12-01 07:22:15
strcpy和memcpy都是标准C库函数,它们有下面的特点。 strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。 已知strcpy函数的原型是:char* strcpy(char* dest, const char* src); memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。 void *memcpy( void * dest , const void * src , size_t count ); char * strcpy ( char * dest, const char * src) // 实现src到dest的复制 {    if ((src == NULL) || (dest == NULL)) //判断参数src和dest的有效性   {        return NULL;   }    char *strdest = dest; //保存目标字符串的首地址    while ((*strDest++ = *strSrc++)!= '\0' ); //把src字符串的内容复制到dest下    return strdest; } void * memcpy ( void *memTo, const void *memFrom, size_t size)

Difference between array and pointer [duplicate]

做~自己de王妃 提交于 2019-12-01 06:21:00
This question already has an answer here: Why does writing to a string literal in this C program segfault? 6 answers What's wrong with my strcpy? [closed] 4 answers Yesterday I had a little trouble with a homemade "strcpy" function. It works now though but I'm a little confused! char* a = "Hello, World!"; //Works char b[] = "Hello, World!"; //Works also strcpy(a, "Hello!"); //Segmentation fault strcpy(b, "Haha!!"); //Works.. Where is the difference? Why does the char pointer cause a "Segmentation fault"? Why does this even work? : char* a = "Haha"; //works a = "LOL"; //works.. char* a = "Hello

作业五——词法分析程序的设计与实现

浪子不回头ぞ 提交于 2019-12-01 04:58:50
实验报告 实验名称:词法分析程序的设计与实现 日期:2019年10月11日 一、实验内容 词法分析程序( Lexical Analyzer )要求: - 从左至右扫描构成源程序的字符流 - 识别出有词法意义的单词( Lexemes ) - 返回单词记录(单词类别,单词本身) - 滤掉空格 - 跳过注释 - 发现词法错误 程序结构: 输入:字符流(什么输入方式,什么数据结构保存) 处理: –遍历(什么遍历方式) –词法规则 输出:单词流(什么输出形式) –二元组 单词类别: 1.标识符(10) 2.无符号数(11) 3.保留字(一词一码) 4.运算符(一词一码) 5.界符(一词一码) 单词符号 种别码 单词符号 种别码 begin 1 : 17 if 2 := 18 then 3 < 20 while 4 <= 21 do 5 <> 22 end 6 > 23 l(l|d)* 10 >= 24 dd* 11 = 25 + 13 ; 26 - 14 ( 27 * 15 ) 28 / 16 # 0 二、程序代码 #include <stdio.h> #include <string.h> #define norw 13 /*关键字个数*/ #define nmax 14 //number的最大位数 #define al 10 //符号的最大长度 符号就是+ #define cxmax

Valgrind Warning: Should I Take It Seriously

*爱你&永不变心* 提交于 2019-12-01 04:49:29
Background: I have a small routine that mimics fgets(character, 2, fp) except it takes a character from a string instead of a stream. newBuff is dynamically allocated string passed as a parameter and character is declared as char character[2] . Routine: character[0] = newBuff[0]; character[1] = '\0'; strcpy(newBuff, newBuff+1); The strcpy replicates the loss of information as each character is read from it. Problem: Valgrind does warns me about this activity, "Source and destination overlap in strcpy(0x419b818, 0x419b819)". Should I worry about this warning? Probably the standard does not

第五次作业

狂风中的少年 提交于 2019-12-01 04:34:28
词法分析程序( Lexical Analyzer )要求: - 从左至右扫描构成源程序的字符流 - 识别出有词法意义的单词( Lexemes ) - 返回单词记录(单词类别,单词本身) - 滤掉空格 - 跳过注释 - 发现词法错误 程序结构: 输入:字符流(什么输入方式,什么数据结构保存) 处理: –遍历(什么遍历方式) –词法规则 输出:单词流(什么输出形式) –二元组 单词类别: 1.标识符(10) 2.无符号数(11) 3.保留字(一词一码) 4.运算符(一词一码) 5.界符(一词一码) 程序代码: #include<iostream> #include<cstdio> #include<cstring> using namespace std; int k=0; struct word { char name[10]; int kind; } word[1000]; char key[35][10]= {"scanf","short","int","long","float","double","char","struct","union", "printf","typedef","const","unsigned","signed","extern","register","static", "volatile","void","if","else","switch",

Valgrind Warning: Should I Take It Seriously

若如初见. 提交于 2019-12-01 02:57:57
问题 Background: I have a small routine that mimics fgets(character, 2, fp) except it takes a character from a string instead of a stream. newBuff is dynamically allocated string passed as a parameter and character is declared as char character[2] . Routine: character[0] = newBuff[0]; character[1] = '\0'; strcpy(newBuff, newBuff+1); The strcpy replicates the loss of information as each character is read from it. Problem: Valgrind does warns me about this activity, "Source and destination overlap

Program aborts when using strcpy on a char pointer? (Works fine on char array)

那年仲夏 提交于 2019-11-30 15:00:53
问题 I'm perplexed as to why the following doesn't work: char * f = "abcdef"; strcpy(f, "abcdef"); printf("%s",f); char s[] = "ddd"; strcpy(&s[0], "eee"); printf("%s", s); In both examples strcpy received a char * yet on the first example it dies a horrible death. 回答1: "abcdef" and "ddd" are string literals which may reside in a read-only section of your address space. char s[] = "ddd" ensures this literal is copied to stack - so it's modifiable. 回答2: char * f = "abcdef"; defines a char pointer to

Program aborts when using strcpy on a char pointer? (Works fine on char array)

陌路散爱 提交于 2019-11-30 13:04:27
I'm perplexed as to why the following doesn't work: char * f = "abcdef"; strcpy(f, "abcdef"); printf("%s",f); char s[] = "ddd"; strcpy(&s[0], "eee"); printf("%s", s); In both examples strcpy received a char * yet on the first example it dies a horrible death. "abcdef" and "ddd" are string literals which may reside in a read-only section of your address space. char s[] = "ddd" ensures this literal is copied to stack - so it's modifiable. char * f = "abcdef"; defines a char pointer to "abcdef" which is located in read-only area so you can't write to this place char s[] = "ddd"; defines a char

'strcpy' with 'malloc'?

时光毁灭记忆、已成空白 提交于 2019-11-30 06:46:23
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); 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 stack like this: char msg[15]; If you malloc the memory you should remember to free it at some point. If you

segmentation fault with strcpy [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-30 05:17:06
This question already has an answer here: Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer 5 answers 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; } seanwatson You haven't initialized *str_new so it is just copying str_old to some random address. You need to do either this: char str_new[100]; or char * str = (char *) malloc(100); You will have to #include <stdlib.h> if you haven't already