字符串的复制strcpy的使用以及自实现

strcpy的自实现

#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char firstName[30] = "jim";
	char lastName[30] = "Green";

	char* p = firstName;
	char* q = lastName;

	while (1)
	{
		if (*q != '\0')
			*p = *q;
		if (*q == '\0')
		{
			*p = *q;
			break;
		}
		p++; q++;
	}
	printf("%s\n", firstName);
	printf("%s\n", lastName);
	return 0;
}

strcpy应用时的注意事项

#include "stdafx.h"
#include <string.h>

int _tmain(int argc, _TCHAR* argv[])
{
	char src[] = "xxxx0000";
	char dest[5]; //dest大小不够只有5  dest中要有足够的空间来保存拷贝内容 
	//本函数有一个特点 拷贝过程中连同'\0'一起拷贝

	strcpy(dest, src); //将src给dest拷贝(越界拷贝)

	printf("src=%s\n", src);  //显示000
	printf("dest=%s\n", strcpy(dest, src));//相当于dest
	printf("dest=%s\n", dest);//显示xxxxoooo


	char dest1[100];
	char dest2[200];

	//返回的 char* 指向dest,可以将函数嵌入其他函数作参数
	//可以实现所谓的连等式,a=b=c;
	strcpy(dest2, strcpy(dest1, src)); //实现连等
	printf("dest2=%s\n", dest2);
	printf("dest1=%s\n", dest1);
	printf("src=%s\n", src);

	//1.strcpy函数声明 char* strcpy(char* dest, char* src); 
	//2.虽然参数为俩个指针类型 但有时候不能直接用指针形式传递 
	//若发生改写只读内容会报错
	//3.最好将strcpy函数理解为 char* strcpy(char dest[],char *src) 类型


	//char* p = "China";
	char* q = "American";
	//strcpy(p, q);  会挂掉,因为系统将"China"编译成只读数据,不能更改

	char p[100]; //将p定义成字符数组后,则可以赋值
	strcpy(p, q);

	printf("p=%s\n", p);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include<iostream> #include<string> #include<malloc.h> using namespace std; struct Node {char name[10]; float price; int num; struct Node *next; }; struct Link {Node *head; int len; }; int search(Link &Q,Node *&p) { Node *q; int i=0; for(q=Q.head;q->next;q=q->next,i++){ if((strcmp(q->name,p->name)==0)){ cout<<"品牌"<<q->name<<endl<<"价格"<<q->price<<endl<<"数量"<<q- >num<<endl; return i;} if(p->price==q->price""p->num==q->num)cout<<"品牌"<<q- >name<<endl<<"价格"<<q->price<<endl<<"数量"<<q->num<<endl; if(p->price<q->price)return i; } return i; } void increase(Link &Q,int i,Node *&p) { Node *q; for(q=Q.head;i>0;i--,q=q->next); if(strcmp(q->name,p->name)==0)q->num+=p->num; else {p->next=q->next;q->next=p;Q.len++;} } void jianli(Link &Q) {Q.head=(Node *)malloc(sizeof(Node)); Node *p; if(!Q.head)exit(1); p=Q.head; strcpy(p->name,"");Q.head->price=0.0;Q.head->num=0;Q.head->next=NULL; p=(Node*)malloc(sizeof(Node)); cout<<"请输入电视机的品牌,价格,数量:"<<endl; cin>>p->name>>p->price>>p->num; while(p->num!=0) { increase(Q,search(Q,p),p); p=(Node*)malloc(sizeof(Node)); cin>>p->name>>p->price>>p->num; } } void decrease(Link &Q,Node *&p) { Node *q; for(q=Q.head;q->next;q=q->next) if(strcmp(q->next->name,p->name)==0)break; if(q->next->num==p->num) {cout<<"该电视机已销售完!"<<endl; q->next=q->next->next;Q.len--; } else q->next->num-=p->num; } void ruku(Link &Q) { Node *p; p=(Node*)malloc(sizeof(Node)); cout<<"请输入电视机的品牌,价格,数量:"<<endl; cin>>p->name>>p->price>>p->num; while(p->num!=0) { increase(Q,search(Q,p),p); p=(Node*)malloc(sizeof(Node)); cin>>p->name>>p->price>>p->num; } } void chuku(Link &Q) { Node *p; p=(Node*)malloc(sizeof(Node)); cout<<"请输入电视机的品牌,价格,数量:"<<endl; cin>>p->name>>p->price>>p->num; while(p->num!=0) { decrease(Q,p); p=(Node*)malloc(sizeof(Node)); cin>>p->name>>p->price>>p->num; } } void chaxun(Link &Q) { Node *p; int i; p=(Node*)malloc(sizeof(Node)); cout<<"查找方式:"<<endl<<"1.按品牌"<<endl<<"2.按价格"<<endl<<"3.按数量"<<e ndl; cout<<"请输入你按何种方式查询:"<<endl; cin>>i; switch(i) {case 1:cout<<"请输入品牌:"<<endl;cin>>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值