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

 
#include<stdio.h>
#define SIZE 10
int 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类数据.

 

 

 

你可能感兴趣的:(【C Prime Plus】学习笔记,Chapter 10,数组和指针之三)