有C基础学C++第二天(数组,函数)

1.一维数组,二维数组,查看占用内存空间,查看元素个数,查看地址

#include<iostream>
using namespace std;

int main() {
	//查看数组中元素个数
	int arr[] = { 1,2,3,4,5,6,7,8 };
	cout << "整个数组占用内存空间为:" << sizeof(arr) << endl;
	cout << "每个元素占用内存空间为:" << sizeof(arr[0]) << endl;
	cout << "数组中元素个数为:" << sizeof(arr) / sizeof(arr[0]) << endl;

	//查看数组首地址
	cout << "数组首地址为:" << arr << endl;
	//第二个元素地址
	cout << "第二个元素地址为:" << &arr[1] << endl;

	//二维数组
	int arr2[2][4] = 
	{
		{ 1, 2, 3, 4 }, 
		{ 5, 6, 7, 8 }
	};
	cout << "整个数组占用内存空间为:" << sizeof(arr2) << endl;
	cout << "每一行占用内存空间为:" << sizeof(arr2[0]) << endl;
	cout << "每个元素占用内存空间为:" << sizeof(arr2[0][0]) << endl;
	cout << "数组中元素个数为:" << sizeof(arr2) / sizeof(arr2[0][0]) << endl;
	cout << "数组中列数为:" << sizeof(arr2[0]) / sizeof(arr2[0][0]) << endl;

	//查看数组首地址
	cout << "二维数组首地址为:" << arr2 << endl;
	//第二个元素地址
	cout << "第二行元素首地址为:" << &arr2[1] << endl;


	system("pause");
	return 0;
}

结果

整个数组占用内存空间为:32
每个元素占用内存空间为:4
数组中元素个数为:8
数组首地址为:00FDF7D8
第二个元素地址为:00FDF7DC
整个数组占用内存空间为:32
每一行占用内存空间为:16
每个元素占用内存空间为:4
数组中元素个数为:8
数组中列数为:4
二维数组首地址为:00FDF7B0
第二行元素首地址为:00FDF7C0
请按任意键继续. . .

2.函数4种常见样式:1.无参无返,2.有参无返,3.无参有返,4.有参有返

#include <iostream>
using namespace std;

//函数常见样式
//1.无参无返
void test01()
{
	cout << "hello" << endl;
}
//2.有参无返
void test02(int a)
{
	cout << "a= " << a << endl;
}
//3.无参有返
int test03()
{
	return 100;
}
//4.有参有返
int test04(int a,int b)
{
	int c = a + b;
	return c;
}

int main() 
{
	test01();
	test02(1000);
	int b = test03();
	cout << b << endl;
	int d = test04(10, 20);
	cout << d << endl;
	system("pause");
	return 0;

}

结果

hello
a= 1000
100
30

3.函数的声明
要想把函数写在main函数的后面,得提前对函数声明,告诉编译器函数的存在,函数的声明可以有多次,但定义只能有一次

int max1(int a, int b);//函数的声明
//要想把函数写在main函数的后面,得提前对函数声明,告诉编译器函数的存在
int max1(int a, int b);
//函数的声明可以有多次,但定义只能有一次

int main() 
{
	
	int c = max1(20, 30);
	cout << c << endl;//结果:30
	system("pause");
	return 0;

}

int max1(int a, int b) 
{
	return a > b ? a : b;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值