strcpy的用法

之前,一直以为strcpy的作用是复制内容

#include <iostream>
#include <cstring>

int main() {
	char str[20] = "I am a student.", *p = str;
	char *q = "You are a teacher.";
	p = p + 7;
	q = q + 10;
	strcpy(p, q);
	puts(str);
	return 0;
}

该程序的运行结果为

strcpy ()函数拷贝之后,是将某一地址的字符串复制到另外一地址中,会将该地址中本身存在的字符串的一部分给覆盖掉。在这个程序里p = p + 7, 指向s前的空格;q = q + 10, 指向t,复制后得到结果

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值