一、指针不等于地址
指针不仅有地址,还有类型,是一个存储了地址的变量,可以改变指向;而地址是一个常量
#include<stdio.h>
#include<stdlib.h>
void main() {
int num=10;
int data=20;
printf("num=%d,&num=%pndata=%d,&data=%pn",num,&num,data,&data);
//用键盘初始化一个指针:初始化一个数据需要数据的地址,初始化一个指针需要指针的地址
int *p;
scanf("%p",&p);//输入num的地址后,p=&num
*p=5;
int *pp;
int pdata;
scanf("%p",&pdata);//输入data的地址后,pdata=&data
pp=(int *)pdata;//把整数转换成指针
*pp=11;
printf("num=%d,&num=%pndata=%d,&data=%pn",num,&num,data,&data);
system("pause");
}
二、指针和字符串数组的区别
#include<stdio.h>
#include<stdlib.h>
void main() {
char *p="abcdefg";//p是一个指针,存储了常量字符串的地址
char str[10]="abcdefg";//str是数组,接受了常量字符串的赋值
printf("%s,%sn",p,str);
printf("%d,%dn",sizeof(p),sizeof(str));
//*p='A';//常量不可修改
str[0]='A';
printf("%s,%sn",p,str);//数组是变量,可以修改
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//c语言头文件,无string类
void main0() {
char str[20]="hello,yincheng ok";
char ch='o';
char *p=str;
while (*p!=' ')
{
if(*p==ch){
char *p1=p;
char *p2=p+1;
}
}
printf("%sn",p);
system("pause");
}
三、删除字符及字符串
1.删除字符
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//c语言头文件,无string类
void main() {
char str[20]="hello,yincheng ok";
char ch='o';
char *p=str;
while (*p!=' ')
{
if(*p==ch){
char *p1=p;
char *p2=p+1;
while (*p2!=' ')
{
*p1=*p2;//字符串向前移动
p1++;
p2++;
}
*p1=' ';//填充结束符
}
p++;
}
printf("%sn",str);
system("pause");
}
2.删除字符串
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>//c语言头文件,无string类
4
5 void main() {
6 char allstr[100]="123456789,hello yincheng, hello c,hello cpp,hello itcast";
7 char str[10]="hello";
8 char *p;
9 while ((p=strstr(allstr,str))!=NULL)//能找到字符串就继续,否则退出循环
10 {
11 int length=strlen(str);//获取要删除字符串的长度
12 char *p1=p;//获取当前位置
13 char *p2=p+length;//获取要删除字符串的后继位置
14 while (*p2!=' ')
15 {
16 *p1=*p2;//根据指针进行字符串拷贝
17 p1++;
18 p2++;
19 }
20 *p1=' ';
21 //一轮循环消灭一个str:代码到此返回第9行,对strstr(allstr,str)进行判断
22 }
23 printf("%sn",allstr);
24
25 system("pause");
26 }
四、检索进程里是否有QQ
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void execmd(char *cmd,char *result){
char buffer[128]={0};//定义一个字符串缓冲区
FILE *pipe=_popen(cmd,"r");//创建一个管道,执行指令,把管道当做文件来处理。r就是把文件按照读的方式来操作
if(pipe==NULL){
printf("运行失败");
return;
}else{
while (!feof(pipe))//判断是否到了文件末尾,没到就继续。feof到了末尾返回非0,否则返回0
{
if (fgets(buffer,128,pipe))//读取文件到缓冲区
strcat(result,buffer);//连接字符串,将结果保存到result
}
_pclose(pipe);//关闭管道
return;
}
}
void main() {
char output[8096]={0};//定义一个字符串,接收输出。必须足够大,否则会溢出
execmd("tasklist",output);//执行指令,将结果保存到output
printf("%sn",output);
if(strstr(output,"QQ.exe")==NULL)
printf("QQ未运行n");
else
printf("QQ已运行n");
system("pause");
}
五、实现memcpy
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void *my_memcpy(void *dst,const void *src,unsigned int Size){
char *tmp=(char *)dst;
const char *s=(char *)src;
while(Size--)
*tmp++=*s++;//进行拷贝,++让指针向前移动
return dst;
}
void main() {
char strA[20]="*****************";
char strB[20]="123456789987654321";
//memcpy(strA,strB,9);
my_memcpy(strA,strB,9);
printf("%sn",strA);
system("pause");
}
原文链接: https://www.cnblogs.com/little-monkey/p/7745360.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/262184
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!