11.10
用sizeof求阵列长度
#include<stdio.h>int main(){int a[3]={1,2,3};printf("size of int:%zu\n",sizeof(a[0]));printf("size of a:%zu\n",sizeof(a));printf("length of a:%zu\n",sizeof(a)/sizeof(a[0]));return 0;}
利用此来计算阵列长度
放在函式中时,计算结果会出现错误,因为函式中的并不是阵列,而是另一种格式
用保留值标记阵列长度
#include<stdio.h>int length(int[]);int main(){int a[4]={3,9,7,-1}; //将-1作为特殊的标记值printf("%d\n",length[a]);return 0;}int length(int a[]){int i=0;while(a[i]!=-1){i++}return i;}
函式中不能包含标记值
可以用标记值来做结束一段语句的标志
函式间传递阵列的原理
其实存储的是阵列第一个元素的起始位置
a[i]的起始位置=的一个元素的起始位置+i*sizeof(int)
字串
#include<stdio.h>void str_print(char str[]){printf("%s\n",str); //用%s来输出}int main(){char str[]="hello world"; //简化前为={'h','e','l','l','o',' ','w','o','r','l','d','\0'}str_print(str); //字串是以'\0'表示结尾的字元阵列return 0;}
