1.1 Êý×éÖ¸Õë
1£©±¾ÖÊ£ºÖ¸Õ룬ָÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍΪÊý×éÀàÐÍ
2£©ÔÐÍ£ºÊý¾ÝÀàÐÍ (*±êʶ·û)[³¤¶È]
3£©ËµÃ÷£ºÖ¸Õë³Æ֮ΪÐÐÖ¸Õë
³¤¶È£º±íʾ¶þάÊý×éµÄÁÐÊý
4£©´óС£º4¸ö×Ö½Ú
char (*p)[4] ;
p + 1;
char a[3][4];
a : Ö¸ÏòµÄ¶ÔÏóµÄÀàÐÍ----?> char [4]
a+1 --->4¸ö×Ö½Ú,Êý×éµÄ&a[1]µÄµØÖ·£¬µÚ1ÐеÄÊ×ÐеØÖ·£¬ ÐдÓ0¿ªÊ¼
a+i ---> iÐеÄÊ×µØÖ· char [4]
char [4] ------>char
a+1 ------------>*(a+1)£¬ ±íʾ1ÐÐÊ×ÁÐÔªËصĵØÖ· charÀàÐÍ
*(a+1)+1 £ºÆ«ÒÆÒ»¸ö×Ö½Ú
#include <stdio.h>
void fun(char (*pf)[4]); int main() { char a[3][4] = {10,11,23,45, 89,90,67,55, 99,88,56,97 }; int i, j; //Ö¸Õë
//char *p = &a[0][0]; char *p = a[0]; //a[0]Ö¸ÏòµÄ¶ÔÏóµÄÀàÐÍΪchar for(i = 0 ; i < 12; i++) printf("%d ",*(p+i)); //½«¶þάÊý×éת±äΪһάÊý×é
printf("\n----------------------\n"); //char (*pf)[4] = a; //aÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍΪchar [4] char (*pf)[4] = &a[0]; //Ö¸ÏòµÄ¶ÔÏóµÄÀàÐÍΪchar [4]
for(i= 0; i < 3; i++) { for(j = 0; j < 4; j++) { //printf("%d ",*(*(pf+i)+j)) ; //printf("%d ",pf[i][j]); printf("%d ",*(pf[i]+j)); } printf("%d\n"); } printf("-------------------------------------\n");
fun(a); printf("-------------------------------------\n");
//-------------------------------------------------
//a £º±¾ÖÊ£ºÊý×飬Êý×éµÄÔªËØa[0],a[1],a[2],,a[i]ÔªËصÄÀàÐÍ char [4] //a[i] : a[i][0] a[i][1] a[i][2] a[i][3]
//a Ö¸ÏòµÄ¶ÔÏóµÄÀàÐÍ char [4]; //a+i : Æ«ÒÆiÐеÄÊ×µØÖ· //char [4] ---->char //a+i--->&a[i] //*(a+i)-->a[i]---->iÐеÄÊ×ÁÐÔªËصĵØÖ· //a[i]+j : ±íʾƫÒÆj¸ö×ֽڵĵØÖ·£¬³ÉΪiÐÐÁÐÔªËصĵØÖ· --->&a[i][j] //*(a[i]+j)---->a[i][j]--->*(*(a+i)+j) //-----------------------------------------------
char b[12] = {10,11,23,45, 89,90,67,55,99,88,56,97}; char (*pt)[4] = b;
//pt = pt +1; //Æ«ÒÆ4¸ö×Ö½Ú //printf("%d ",**pt); //
for(i = 0; i< 12; i++) { printf("%d ",*(*pt + i)); } printf("-------------------------------\n");
}
void fun(char (*pf)[4]) { int i,j;
for(i= 0; i < 3; i++) { for(j = 0; j < 4; j++) { //printf("%d ",*(*(pf+i)+j)) ; //printf("%d ",pf[i][j]); printf("%d ",*(pf[i]+j)); } printf("%d\n"); }
} |
Á·Ï°£ºÐ´Ò»¸ö×Óº¯Êý£¬½«3*3µÄ¾ØÕóתÖ㬲¢ÒÔÖ¸ÕëµÄÐÎʽ²Ù×÷
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
a[0][1] a[1][0]
a[0][2] a[2][0]
a[1][2] a[2][1]
i != j
#include <stdio.h>
void swap(int (*p)[3]); int main() { int a[3][3] = { 1,2,3, 4,5,6, 7,8,9 }; int i,j;
swap(a);
for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) {
printf("%d ",*((*(a+i))+j)); } printf("\n"); }
}
void swap(int (*p)[3]) { int i,j; int tmp;
for(i = 0; i < 3; i++) { for(j = 0; j < i; j++) { if(i != j) { tmp = *((*(p+i))+j); *((*(p+i))+j) = *((*(p+j))+i); *((*(p+j))+i) = tmp;
} } } } |
1.2 Ö¸ÕëÊý×é
1£©±¾ÖÊ£ºÊý×飬Êý×éÔªËصÄÀàÐÍΪָÕëÀàÐÍ£¬ÔªËØ´æ´¢µØÖ·
2£©¸ñʽ£ºÊý¾ÝÀàÐÍ * ±êʶ·û[³¤¶È];
3£©ËµÃ÷£ºÊý¾ÝÀàÐͱíʾµÄÊÇÖ¸ÕëÖ¸ÏòµÄÊý¾ÝµÄÀàÐÍ
³¤¶È£ºÖ¸ÕëµÄ¸öÊý£¬Êý×éµÄÔªËØÊÇÖ¸Õë
4£©´óС£º³¤¶È*4
5£©ÓÃÓÚ£ºÒ»°ãÓÃÓÚÖ¸Ïò×Ö·û´®
#include <stdio.h>
int main() { int a,b,c,d; //ÓÃÖ¸ÕëÖ¸Ïòabcd£¬ÐèÒª4¸öÖ¸Õë //int *p1 = &a,*p2 = &b,*p3 = &c,*p4 = &d;
//int* p[4] = {&a,&b,&c,&d}; //±íʾÓÐ4¸öÆäÔªËØÀàÐÍΪָÕëÀàÐ͵ÄÔªËØ int *p[4]; //Ö¸ÕëÊý×é
p[0] = &a; p[1] = &b; p[2] = &c; p[3] = &d;
int i; //scanf("%d %d %d %d",&a,&b,&c,&d); for(i = 0; i < 4; i++) scanf("%d",p[i]); //p[i] Ö¸Õë printf("%d %d %d %d",a,b,c,d); } |
¶þάÊý×é´æ´¢¶à¸ö×Ö·û´®£¬Ðбíʾ×Ö·û´®µÄ¸öÊý£¬ÁбíʾµÄÿ¸ö×Ö·û´®ËùÄÜÈÝÄɵÄ×î´óµÄ×Ö·ûµÄ¸öÊý¡¢
¶þάÊý×é³õʼ»¯£¬×Ö·û´®µÄ³¤¶È²»Ò»Ñù
char a[4][20] = {¡°jhdhda¡±, ¡°dsg¡±,¡±dsjghsjlgd¡±,¡±yub¡±}; //ÀË·Ñ¿Õ¼ä
Ö¸ÕëÖ¸Ïò×Ö·û´®£¬×Ö·û´®µÄ³¤¶ÈÊÜÏÞÖÆ£¬
¿ÉÒÔʹÓÃÖ¸ÕëÊý×éÖ¸Ïò¶à¸ö×Ö·û´®£¬Êý×éµÄÔªËصĸöÊý±íʾµÄ×Ö·û´®µÄ¸öÊý
char *p = ¡°jhdhda¡±;
#include <stdio.h>
int main() { char *pf[4] = {"hagdukAGUYgwyuADOIhu", "jhdsjsa", "jsdh", "hdsg sjg ysgd uygsd" }; //Ö¸ÕëÖ¸Ïò×Ö·û´®£¬Ö¸Õë´æ´¢×Ö·û´®Ê××Ö·ûµÄµØÖ· pf[1] = "hello world"; pf[3] = "welcome";
int i;
for(i = 0; i < 4; i++) printf("%s\n",pf[i]);
//ÖÕ¶ËÊäÈë
//char str[30]; // // pf[2] = str; //Ö¸ÕëÖ¸ÏòÒ»¿é¿ÉÒÔ¶ÁдµÄÁ¬ÐøµÄÒ»¿é¿Õ¼ä // gets(pf[2]);
char buf[4][30]; //buf[0] &buf[0][0]Ö¸ÏòµÄ¶ÔÏóµÄÀàÐÍΪchar
for(i = 0; i < 4; i++) { pf[i] = buf[i]; //pf[0] = buf[0] gets(pf[i]); //½«ÖÕ¶ËÊäÈëµÄ×Ö·û´®´æ´¢pf[i]Ö¸ÕëÖ¸ÏòµÄ¿Õ¼ä }
printf("------------------\n"); for(i = 0; i < 4; i++) printf("%s\n",pf[i]); } |
1.3 Ö¸Õ뺯Êý
1£©±¾ÖÊ£ºº¯Êý£¬º¯ÊýµÄ·µ»ØÖµÀàÐÍΪָÕëÀàÐÍ£¬º¯ÊýÐèÒª·µ»ØµÄÊÇÒ»¸öµØÖ·
2£©¸ñʽ£ºÊý¾ÝÀàÐÍ * º¯ÊýÃû(²ÎÊýÁбí)
3£©ËµÃ÷£ºÊý¾ÝÀàÐÍ£ºÖ¸ÕëÖ¸ÏòµÄÊý¾ÝµÄÀàÐÍ
Ö¸Õ뺯Êý¸úÇ°ÃæѧϰµÄº¯Êýд·¨Ò»ÖÂ
1.4 º¯ÊýÖ¸Õë
1£©±¾ÖÊ£ºÖ¸Õ룬ָÕëÖ¸ÏòµÄ¶ÔÏóÊǺ¯Êý£¬º¯ÊýÃû´ú±íº¯ÊýµÄÈë¿ÚµØÖ·
2£©¸ñʽ£ºÊý¾ÝÀàÐÍ (*Ö¸ÕëÃû)(²ÎÊýÁбí)
3£©ËµÃ÷£º
Êý¾ÝÀàÐÍ£ºº¯Êý·µ»ØÖµÀàÐÍ£¬Òª¸úÖ¸ÕëÖ¸ÏòµÄº¯ÊýµÄ·µ»ØÖµÀàÐͱ£³ÖÒ»ÖÂ
²ÎÊýÁÐ±í£º¿ÉÒÔдҲ¿ÉÒÔ²»Ð´£¬Èç¹ûÐèҪд²ÎÊýÁÐ±í£¬ÔòÖ»ÐèҪд²ÎÊýµÄÊý¾ÝÀàÐͼ´¿É
4£©º¯ÊýÖ¸Õëµ÷Óú¯ÊýµÄ¸ñʽ£º
Ö¸ÕëÃû(ʵ²Î)
(*Ö¸ÕëÃû)(ʵ²Î)
#include <stdio.h>
int str_len(char *str); int str_cmp(const char *buf,const char *str);
char *str_cat(char *str,const char *buf); char * str_cpy(char *str,const char *buf);
int main() {
char buf[50] = {"hello world!"}; int len; #if 0
int (*p_strint)(); //Èç¹ûÖ¸ÕëÖ¸Ïò¶à¸öº¯ÊýµÄʱºò£¬º¯ÊýµÄ²ÎÊý¸öÊýÒÔ¼°²ÎÊýÀàÐͲ»Ò»Ö£¬Ôò´Ëʱ²»ÐèҪд²ÎÊý //ÔÚ½øÐÐÓÃÖ¸Õëµ÷Óú¯ÊýµÄʱºò£¬Æä²ÎÊýµÄ´«µÝÊÇ°´ÕÕÖ¸ÕëÖ¸ÏòµÄº¯ÊýµÄ²ÎÊý½øÐд«µÝµÄ //Èç¹ûº¯ÊýÖ¸ÕëµÄ²ÎÊýÁбíÓÐд²ÎÊý£¬ÔòÓÃÖ¸Õëµ÷Óú¯ÊýµÄʱºò£¬Æä²ÎÊýµÄ´«µÝÐèÒª°´ÕÕ //º¯ÊýÖ¸ÕëµÄ²ÎÊýÁбíµÄ²ÎÊý½øÐд«µÝ
//--------------Ö¸ÕëÖ¸ÏòÇó×Ö·û´®³¤¶Èº¯Êý---------------------------
//len = str_len("shag jhdsg");
p_strint = str_len; //Ö¸ÕëÖ¸Ïò str_lenº¯Êý
//len = p_strint("shag jhdsg"); //½«Ö¸ÕëÃûµ±³Éº¯ÊýÃûµ÷Óú¯Êý len = (*p_strint)("shag jhdsg"); //Äܹ»ºÜÈÝÒ׿´³öÀ´ÊÇÓÃÖ¸Õëµ÷Óú¯Êý
printf("%d\n",len);
//-----------------------Ö¸ÕëÖ¸Ïò×Ö·û´®±È½Ïº¯Êý--------------------
p_strint = str_cmp; //Ö¸ÕëÖ¸Ïòstr_cmpº¯Êý
len = (*p_strint)("hello","hell");
printf("%d\n",len);
#else
char * (*p_strfun)(char *,const char *); //µ±Ö¸ÕëÖ¸Ïò¶à¸öº¯ÊýµÄʱºò£¬Èç¹ûº¯ÊýµÄ²ÎÊý¸öÊýÒÔ¼°ÀàÐÍÒ»Ö£¬¿ÉÒÔ¶¨Ò庯ÊýÖ¸ÕëµÄ²ÎÊýÁбí char *p ;
p_strfun = str_cpy; //Ö¸Ïò×Ö·û´®¿½±´º¯Êý
p = (*p_strfun)(buf,"abcdefghi jkl");
puts(buf); puts(p);
//----------------------------------------------------
p_strfun = str_cat;
p = p_strfun(buf," hello world"); puts(p); puts(buf);
#endif }
int str_len(char *str) { int i = 0;
while(*str) { i++; str++; }
return i; }
char * str_cpy(char *str,const char *buf) { char *st = str;
if(str == NULL || buf == NULL) return NULL;
while(*buf) { *str = *buf; str++; buf++; } *str = '\0';
return st; }
char *str_cat(char *str,const char *buf) { char *st = str;
if(str == NULL || buf == NULL) return NULL;
while(*str) { str++; } while(*buf) { *str = *buf; str++; buf++; } *str = '\0';
return st; }
int str_cmp(const char *buf,const char *str) { if(str == NULL || buf == NULL) return NULL;
while(*buf || *str) { if(*buf > *str) return 1; else if(*buf < *str) return -1; buf++; str++; }
return 0; } |
1.5 ¶þ¼¶Ö¸Õë
1£©±¾ÖÊ£ºÖ¸Õ룬ָÏòµÄ¶ÔÏóµÄÀàÐÍΪָÕëÀàÐÍ
2£©ÔÐÍ£ºÊý¾ÝÀàÐÍ* *Ö¸ÕëÃû
3£©ËµÃ÷£ºÊý¾ÝÀàÐÍÒ»¼¶Ö¸ÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐ;ö¶¨
4£©´óС£º4¸ö×Ö½Ú
#include <stdio.h> int main() { int a = 10;
int *p = &a;
int* *pf = &p; printf("%#X\n",pf); //´òÓ¡pfµÄÖµ--->pµÄµØÖ· printf("%#X\n",&p); //Ö¸Õë±äÁ¿pµÄµØÖ· printf("%#X\n",p); //´òÓ¡pµÄÖµ£¬--->aµÄµØÖ· printf("%#X\n",&a); //aµÄµØÖ· printf("%#X\n",*pf); //È¡pfÖ¸ÏòµÄ¿Õ¼äµÄÀïÃæµÄÄÚÈÝ--->pµÄÖµ-->aµÄµØÖ· printf("%#X\n",*p); // È¡pÖ¸ÏòµÄ¿Õ¼äÀïÃæµÄÄÚÈÝ£¬aµÄÖµ printf("%#X\n",**pf); //aµÄÖµ } |
¶þ¼¶Ö¸ÕëµÄÓ÷¨
#include <stdio.h>
void fun(char* *p);
int main() { char *pf[4] = {"hagdukAGUYgwyuADOIhu", "jhdsjsa", "jsdh", "hdsg sjg ysgd uygsd" }; //Ö¸ÕëÊý×é £¬ÓÐ4¸öÔªËØ£¬ÔªËصÄÀàÐÍΪ char * //Êý×éµÄÊ×ÔªËصØÖ· pf£¬&pf[0] --->Ö¸ÏòµÄÊ×ÔªËصÄÀàÐÍ
fun(pf); }
void fun(char**p) //Ö¸ÕëÖ¸ÏòÊ×ÔªËØ { int i;
for(i = 0; i < 4; i++ ) { printf("%s\n",*(p+i)); //pÊÇÊ×ÔªËصĵØÖ·--&p[0]-- p[0] //*(p+i) = p[i] } //p+i £º Æ«ÒÆÒ»¸öÔªËØ×Ö½Ú´óС£¬ÔªËصÄÀàÐÍ char *
printf("%#p\n",p); printf("%#p\n",p+1); } |
#include <stdio.h>
int main() { char* a[]={"Pascal","C language","dBase","Coble"}; //Ö¸ÕëÊý×é char **p; int j;
p=a+3; //&a[3] ÔªËصÄÀàÐÍΪchar *
// printf("%s\n",*(p-1));
for(j=3;j>=0;j--) printf("%s\n",*(p--)); //(p-1)--->&a[2] //*(p-1) -->a[2]--->Ö¸ÕëÖ¸Ïò"dBase", } |