【C Prime Plus】学习笔记,Chapter 10,数组和指针之三

来源:互联网 发布:淘宝帐户绑定手机号码 编辑:程序博客网 时间:2024/06/08 06:21
 
#include<stdio.h>#define SIZE 10int sum(int [],int);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 %ld bytes.\n",sizeof(marbles)); //---return 0;}    int sum(int ar[],int n){     int i; int total=0; for(i=0;i<n;i++)total+=ar[i] ;printf("the size of ar is %ld bytes.\n",sizeof ar);  //--      return total;}


输出结果是:

the size of ar is 4 bytes.
the total number of marbles is 190.
the size of marbles is 40 bytes.

 

//--

因为marbles 包含了10个int类型的元素,每个int元素占4个字节(byte),一共是40个字节;

而ar虽然在函数的实现部分当做一个数组在用,但是ar并非数组本身,ar是指向marbles的第一个元素的指针,指针就是4个字节的int类数据.