C++指针,结构和联合

本文详细介绍了C++中的指针、结构和联合。通过实例展示了二维数组的定义,以及如何使用指针、流程控制语句(如for、while、do-while)、计算圆周率和立方根的算法。此外,还讨论了指针的用法,包括取地址运算符、间接运算符、const指针和引用,并解释了结构和联合的概念以及它们在定义和使用时的注意事项。
摘要由CSDN通过智能技术生成

例:二维数组的定义;
#include <iostream.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */

int main()
{
int b[2][3]={ {1,2,3},{4,5,6}};
cout<<“数据元素b[0][0]值为”<<b[0][0]<<endl;

cout<<"数据元素b[0][3]值为"<<b[0][3]<<endl;

cout<<"数据元素b[1][1]值为"<<b[1][1]<<endl; 

cout<<"数据元素b[2][3]值为"<<b[2][3]<<endl;

b[2][3]=2.8; 

cout<<"数据元素b[2][3]值为"<<b[2][3]<<endl;

return 0;

}


数据元素b[0][0]值为1
数据元素b[0][3]值为4
数据元素b[1][1]值为5
数据元素b[2][3]值为0
数据元素b[2][3]值为2


Process exited after 0.01683 seconds with return value 0
请按任意键继续. . .

一:C++流程控制语句:
1.流程语句用于控制程序中各语句的执行功能,C++提供的流程控制语句有选择语句(if,switch),循环语句(for,while,do-while)和跳转语句(continue,break,goto,return)等三种语句;

例:输出十道减法题(for循环);
#include <iostream.h>
#include<stdlib.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system(“pause”) or input loop */
int main()
{
int a,b,t,i;
for(i=1;i<=10;i++)
{
a=rand();
b=rand();
if(a<b)
{
t=a;a=b;b=t;
}
cout<<a<<’-’<<b<<’=’<<endl;
}
return 0;
}


18467-41=
26500-6334=
19169-15724=
29358-11478=
26962-24464=
28145-5705=
23281-16827=
9961-491=
11942-2995=
5436-4827=


Process exited after 0.02377 seconds

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值