指针数组
int * b[10];
int **pp1 = b;
int **pp2 = &b[0];
b是一个指针数组,里面存放的是指针,使用二级指针来找到数组的地址。
测试源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
char chr[] = "hello world";//以字符串初始化,自动隐藏结束符‘\0’
int chr_sizeof,chr_strlen;
chr_sizeof = sizeof(chr);
chr_strlen = strlen(chr);
printf("chr_sizeof = %d\n",chr_sizeof);
printf("chr_strlen = %d\n",chr_strlen);
puts("***************************************************************");
int a[10] = {1,1,2,3,4,5,8};
//a是首元素的地址,元素类型为int
int *p1 = a;
int *p2 = &a[0];
printf("a的地址为%p,a[0]的地址为:%p\n",a,&a[0]);
printf("p1的地址为%p,p2的地址为:%p\n",p1,p2);
//b是首元素的地址,元素类型为int *
int * b[10];
int **pp1 = b;
int **pp2 = &b[0];
printf("b的地址为%p,b[0]的地址为:%p\n",b,&b[0]);
printf("pp1的地址为%p,pp2的地址为:%p\n",pp1,pp2);
return 0;
}
测试结果
#include <string.h>
#include <stdio.h>
#define DIM(a) (sizeof(a)/sizeof(*a))
//求关键字在表中的位置
//一个入口 多个出口
int searcheKeyTable(const char* table[], const int size, const char* key, int *pos)
{
int rv = 0;
int i = 0;
int inum = 0;
if (table==NULL || key==NULL || pos==NULL)
{
rv = -1;
printf("func searcheKeyTable:%d", rv);
return rv;
}
//间接的证明 数组做函数参数的退化
inum = (sizeof(table)/sizeof(*table));
for(i=0; i<size; i++)
{
if( strcmp(key, table[i]) == 0 )
{
*pos = i;
//break;
return rv;
}
}
//没有找到返回-1
if (i == size)
{
*pos = -1;
}
return rv;
}
int demo(void)
{
int inum = 0;
int pos = 0;
int a[10];
int i = 0;
//指针数组
char* c_keyword[] = {
"while",
"case",
"static",
"do"
};
searcheKeyTable(c_keyword, DIM(c_keyword),"do", &pos);
//searcheKeyTable(c_keyword, (sizeof(c_keyword)/sizeof(*c_keyword)),"do", &pos);
printf("pos:%d\n", pos);
//searcheKeyTable(NULL, DIM(c_keyword),"do", &pos);
//printf("pos:%d\n", pos);
searcheKeyTable(c_keyword, DIM(c_keyword), "static", &pos);
printf("pos:%d\n", pos);
return 0;
}
int main(int argc, const char * argv[])
{
int inum = 0;
int pos = 0;
int a[10];
int i = 0;
//指针数组 自我结束能力
char* c_keyword[] = {
"while",
"case",
"static",
"do",
'\0'
};
char* c_keyword2[] = {
"while",
"case",
"static",
"do",
0
};
char* c_keyword3[] = {
"while",
"case",
"static",
"do",
NULL
};
for (i=0; c_keyword[i] != NULL; i++)
{
printf("%s\n", c_keyword[i]);
}
printf("\n....\n");
for (i=0; c_keyword2[i] != NULL; i++)
{
printf("%s\n", c_keyword2[i]);
}
printf("\n....\n");
for (i=0; c_keyword3[i] != NULL; i++)
{
printf("%s\n", c_keyword3[i]);
}
return 0;
}