#include <stdio.h>
int main(){
int n=0;
char c;
double x;
printf("%d %d %d\n",sizeof(n),sizeof(c),sizeof(x));
return 0;
}
输出的结果为:
4 1 8
Program ended with exit code: 0
#include <stdio.h>
int main(){
int *p1,*p2;
int n=4;
char *pc1,*pc2;
p1=(int *)100; //地址p1为100;
p2=(int *)200; //地址p2为200;
printf("1)%d\n",(p2-p1)); //输出的结果为25,因为(200-100)/sizeof(int)
pc1=(char *)p1; //地址pc1为100;
pc2=(char *)p2; //地址p2为200;
printf("2)%d\n",(pc2-pc1)); //输出的结果为100,因为(200-100)/sizeof(char)
printf("3)%d\n",(p2+n)-p1); //输出结果为29;
int *p3=p2+n; //p2+n是一个指针,可以用它给p3赋值;
printf("4)%d\n",p3-p1); //输出结果为29;
printf("5)%d\n",(pc2-10)-pc1); // 输出结果为90;
return 0;
}
1)25
2)100
3)29
4)29
5)90
Program ended with exit code: 0
可以用“NULL”关键字对任何类型的指针进行赋值。NULL实际上
就是整数0,值为NULL的指针就是空指针:
int * pn = NULL; char * pc = NULL; int * p2 = 0;
指针可以作为条件表达式使用。如果指针的值为NULL,则相当于为 假,值不为NULL,就相当于为真。
if§<—>if(p!=NULL);
if(!p)<—>if( p==NULL )
指针作为函数参数
#include <stdio.h>
void swap(int *p1,int *p2){
int tmp=*p1; //将p1指向的变量的值,赋给tmp;
*p1=*p2; //将p2指向的变量的值,赋给p1所指向的变量;
*p2=tmp; //将tmp的值赋给p2指向的变量;
}
int main(){
int m=3,n=4;
swap(&m,&n); //使得p1指向m,p2指向n;
printf("%d %d\n",m,n);
}
指针和数组
数组的名字是一个指针常量 指向数组的起始地址;
T a[N];
a的类型是 T *
可以用a给一个T * 类型的指针赋值
a是编译时其值就确定了的常量,不能够对a进行赋值;
作为函数形参时, T *p 和 T p[ ] 等价
#include <stdio.h>
int main(){
int a[100];
int *p;
p=a; //p指向数组a的起始地址,亦即p指向了a[0]
*p=10; //使得a[0]=10;
*(p+1)=20; //使得a[1]=20;
p[0]=30; //p[i]=*(p+i),a[0]=30;
p[4]=40; //a[4]=40;
for (int i=0;i<10;++i){ //对数组的前10个元素进行赋值;
*(p+i)=i;
printf("%d ",*(p+i)); //0 1 2 3 4 5 6 7 8 9
}++p; //p指向a[1]
printf("\n");
printf("%d\n",p[0]); //输出1 p[0]=*p,p[0]即是a[1]
p=a+6; //p指向a[6]
printf("%d\n",*p); //输出6;
return 0;
}
#include <stdio.h>
void reverse(int *p,int size){
for(int i=0;i<size/2;++i){ //颠倒一个数组;
int tmp=p[i];
p[i]=p[size-1-i];
p[size-1-i]=tmp;
}
}
int main(){
int a[5]={1,2,3,4,5};
reverse(a,sizeof(a)/sizeof(a[0]));
for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){
printf("%d ",a[i]);
}
printf("\n");
return 0;
}
输出的结果为:
5 4 3 2 1
Program ended with exit code: 0
指针和二维数组
指向指针的指针
#include <stdio.h>
int main(){
int **pp; //指向int*类型指针的指针
int *p;
int n=1234;
p=&n; //p指向n
pp=&p; //pp指向p
printf("%d\n",*(*pp)); //*pp是p, 所以*(*pp)就是n
return 0;
}
指针和字符串
字符串常量的类型就是 char *
字符数组名的类型也是 char *
#include <stdio.h>
int main(){
char *p="please input your name:\n";
printf("%s",p);
char name[20]="name";
//直接写成char name[20]也是可以实现效果;
char *pname=name; //储存输入的字符串。
scanf("%s",pname);
printf("your name is %s\n",pname);
return 0;
}
please input your name:
grape
your name is grape
Program ended with exit code: 0
字符串操作
#include <stdio.h>
#include<string.h>
int main(){
char s1[100]="12345";
char s2[100]="abcdefg";
char s3[100]="ABCDE";
strncat(s1,s2,3);
printf("1)s1=%s\n",s1); // 1)s1=12345abc
strncpy(s1,s3,3);
printf("2)s1=%s\n",s1); //2)s1=ABC45abc
strncpy(s2,s3,6);
printf("3)s2=%s\n",s2); //3)s2=ABCDE
printf("4)strncmp(s1,s3,3)=%d\n",strncmp(s1,s3,3)); //strncmp(s1,s3,3)=0
char *p=strchr(s1,'B');
/*在s1中查找 'B'第一次出现的位置,如果找到,就返回指向该位置的char*指针;如果str中不包含字符,则返回NULL*/
if(p) { //等价于 if( p!= NULL)
printf("5) *p=%p,p=%s\n",*p,p); //5) *p=0x42,p=BC45abc
printf("6) p-s1=%i\n",p-s1); //p-s1=1
}else
printf("5)not found\n");
p=strstr(s1,"45a");
/*har * strstr(const char * str, const char * subStr); 寻找子串subStr在str中第一次出现的位置。如果找到,就返回指向该位置的指针;如
果str不包含字符串subStr,则返回NULL*/
if(p)
printf("7)p-s1=%i,p=%s\n",p-s1,p); //)p-s1=3,p=45abc
else
printf("8)not found");
//以下演示strtok用法:
printf("stroke usage demo:");
char str[]="- this,a sample string, ok.";
//下面要从str逐个抽取出被" ,.-"这几个字符分隔的字串
p=strtok(str," ,.-");
while(p!=NULL){
printf("%s\n",p);
/*troke usage demo:this
a
sample
string
ok*/
p=strtok(NULL," ,.-"); //后续调用,第一个参数必须是NULL
}
return 0;
}
Void指针
void * p;
可以用任何类型的指针对 void 指针进行赋值或初始化:
double d = 1.54; void * p = & d; void * p1;
p1 = & d;
因 sizeof(void) 没有定义,所以对于 void * 类型的指针p,
*p 无定义
++p, --p, p += n, p+n,p-n 等均无定义
内存操作库函数memset
void * memset(void * dest,int ch,int n);
将从dest开始的n个字节,都设置成ch。返回值是dest。ch只有最低的字节起 作用。
例:将szName的前10个字符,都设置成’a’:
char szName[200] = “”; memset( szName,‘a’,10); cout << szName << endl; =>aaaaaaaaaa
用memset函数将数组内容全部设置成0:
int a[100]; memset(a,0,sizeof(a));
则数组a的每个元素都变成0
void * memcpy(void * dest, void * src, int n);
将地址src开始的n个字节,拷贝到地址dest。返回值是dest。
int a1[10];
int a2[10];
memcpy( a2, a1, 10*sizeof(int));
函数指针
#include<stdio.h>
void printmin(int a,int b){
if(a<b)
printf("%d",a);
else
printf("%d",b);
}
int main(){
int x=4;
int y=5;
printmin(x,y);
return 0;
}