指针不仅有地址,还有类型,是一个存储了地址的变量,可以改变指向;而地址是一个常量
#include<stdio.h> #include<stdlib.h> void main() { int num=10; int data=20; printf("num=%d,&num=%p\ndata=%d,&data=%p\n",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=%p\ndata=%d,&data=%p\n",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,%s\n",p,str); printf("%d,%d\n",sizeof(p),sizeof(str)); //*p=‘A‘;//常量不可修改 str[0]=‘A‘; printf("%s,%s\n",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!=‘\0‘) { if(*p==ch){ char *p1=p; char *p2=p+1; } } printf("%s\n",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!=‘\0‘) { if(*p==ch){ char *p1=p; char *p2=p+1; while (*p2!=‘\0‘) { *p1=*p2;//字符串向前移动 p1++; p2++; } *p1=‘\0‘;//填充结束符 } p++; } printf("%s\n",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!=‘\0‘) 15 { 16 *p1=*p2;//根据指针进行字符串拷贝 17 p1++; 18 p2++; 19 } 20 *p1=‘\0‘; 21 //一轮循环消灭一个str:代码到此返回第9行,对strstr(allstr,str)进行判断 22 } 23 printf("%s\n",allstr); 24 25 system("pause"); 26 }
#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("%s\n",output); if(strstr(output,"QQ.exe")==NULL) printf("QQ未运行\n"); else printf("QQ已运行\n"); system("pause"); }
#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("%s\n",strA); system("pause"); }
[c/c++] programming之路(29)、阶段答疑
原文:http://www.cnblogs.com/little-monkey/p/7745360.html