- data + 2 == &data[2];
- *(data + 2) == data[2];
- #include<stdio.h>
- #define SIZE 10
- int main(void){
- int marbles[SIZE]={20,10,5,39,4,16,19,26,31,20};
- long answer;
- answer = sum(marbles,SIZE);//将数字大小当做第二个参数传递
- printf("The total number of marbles is %ld\n",answer);
- printf("The size of marbles is %u bytes.\n",sizeof marbles);//此处的marbles是一个数组,我们求出的是整个数组的字节数
- return 0;
- }
- int sum(int ar[],int n){//此时的ar并不是数组,它是一个指向marbles的首元素的指针
- int i; //此时的n是该数组的大小
- //我们可以使用int *ar来替换int ar[]
- int total = 0;
- for(i=0;i<n;i++){
- total += ar[i];
- }
- printf("The size of ar is %u bytes.\n",sizeof ar);指针的大小为4个字节
- return total;
- }
- #include<stdio.h>
- #define SIZE 10
- long sump(int *start,int *end);
- int main(void){
- int marbles[SIZE] = {20,10,5,39,4,16,19,26,31,20};
- long answer;
- answer = sump(marbles,marbles+SIZE);
- printf("The total number of marbles is %ld",answer);
- return 0;
- }
- long sump(int *start,int *end){ //end是指向的是数组最后一个元素后面的一个元素
- long result = 0;
- while(start<end){
- result += *start++;
- }
- return result;
- }