指针

const和指针

#define PI 3.14    不会进行类型检查  编译时

const float PI=3.14 编译时会进行类型检查

 

int const a;

const int a;

 

const int *p;  //常量指针

int const *p;  //指针指向一个常量,指针指向可以改变

函数封装时用得比较多

FILE *fopen(const char *path, const char *mode);

void *memcpy(void *dest, const void *src, size_t n);

#include<stdio.h>
 
int main()
{   
    int i=0;
    const int *p=&i;
 
    printf("%d\n",*p);
  
 //False    *p=10;  //error: assignment of read-only location ‘*p’
 //True    i=10;
    printf("%d\n",*p);
}
 #include<stdio.h>
 int main()
 {
     const int i=10;
     
     int *p=&i;  // warning: initialization discards ‘const’ qualifier from pointer target type [enabled by def]
     *p=0;   //i的值被改变
     printf("%d\n",*p);
 }   

int * const p;  //指针常量  

指针是常量,指针指向不可以改变,但指向的值可以改变

 

const int * const p; //常量指针常量

都不可以改变

 

数组指针与指针数组

int (*p)[n] 数组指针

int *arr[3] 指针数组

#include<stdio.h>
#include<stdlib.h>

int main()
{
	char *name[5]={"Follow me","Basic","Great","Fortran","Computer"};
	int i;
	int j,k;
	char *temp;


	for(i=0;i<5-1;i++)
	{
		k=i;
		for(j=i+1;j<5;j++)
		{
			if(strcmp(name[k],name[j])>0)
				k=j;
		}
		if(k!=i)
		{
			temp=name[i];
			name[i]=name[k];
			name[k]=temp;
		}
	}


	
	for(i=0;i<5;i++)
	{
		puts(name[i]);
	}

	exit(0);
}

 

多级指针:常用二级指针

 

函数与指针

指针函数

     返回值* 函数名(形参){};  //返回值为指针

函数指针

函数返回值类型 (*fun)[形参表]

#include<Stdio.h>
#include<stdlib.h>

int add(int a,int b)
{
	return a+b;
}

int main()
{
	int a=3,b=5;
	int ret;
	int (*p)(int , int);
	int res;

	p=add;   //add本身就是地址

	ret=add(a,b);
	printf("%d\n",ret);

	res=p(a,b);
	printf("%d\n",res);

	exit(0);
}

函数指针数组

类型 (*数组名【下标】)(参数)

int  (*fun[n] )(int ,int)

指向指针函数的函数指针数组

int *(*fun[n])(int ,int)

posted @ 2015-12-26 16:12  让梦想远航……  阅读(166)  评论(0)    收藏  举报