测试源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
//void print_buff(int buff[],int len)
//void print_buff(int buff[10],int len)
void print_buff(int *buff,int len)
{
int i = 0;
for(i=0;i<len;++i)
{
printf("buff[%d] = %d\n",i,buff[i] );
}
return ;
}
//void print_buf(int* buff[],int len)
//void print_buf(int *buff[10],int len)
void print_buf(char **buff,int len)
{
int i = 0;
for(i=0;i<len;++i)
{
printf("buf[%d] = %s\n",i,buff[i] );
}
return ;
}
int main(int argc, const char *argv[])
{
int buff[10] = {1,2,3,4,5,6,7,8,9,10};
int n = sizeof(buff) / sizeof(buff[0]);
print_buff(buff,n);
print_buff(&buff[0],n);
printf("&buff = %lu\n",&buff);
printf("&buff+1 = %lu\n",&buff +1);
char *buf[10] = {"11","22","33","44","55","66","77","88","99","1010"};
int n1 = sizeof(buf) / sizeof(buf[0]);
print_buf(buf,n);
printf("&buf = %lu\n",&buf);
printf("&buf+1 = %lu\n",&buf +1);
printf("sizeof(char *) = %d\n",sizeof(char *));
return 0;
}
测试结果
我的为64位系统,所以指针大小为8个字节。
mint@linux:~$ ./a.out
buff[0] = 1
buff[1] = 2
buff[2] = 3
buff[3] = 4
buff[4] = 5
buff[5] = 6
buff[6] = 7
buff[7] = 8
buff[8] = 9
buff[9] = 10
buff[0] = 1
buff[1] = 2
buff[2] = 3
buff[3] = 4
buff[4] = 5
buff[5] = 6
buff[6] = 7
buff[7] = 8
buff[8] = 9
buff[9] = 10
&buff = 140726845473264
&buff+1 = 140726845473304
buf[0] = 11
buf[1] = 22
buf[2] = 33
buf[3] = 44
buf[4] = 55
buf[5] = 66
buf[6] = 77
buf[7] = 88
buf[8] = 99
buf[9] = 1010
&buf = 2242019872
&buf+1 = 2242019952
sizeof(char *) = 8
关键代码分析
以下等效
//void print_buff(int buff[],int len)
//void print_buff(int buff[10],int len)
void print_buff(int *buff,int len)
//void print_buf(int* buff[],int len)
//void print_buf(int *buff[10],int len)
void print_buf(char **buff,int len)