-
在堆中申请100个char类型的内存,拷贝Hello imooc字符串到分配的堆中的内存中,打印字符串,最后释放内存。#include <string.h> #include <iostream> using namespace std; int main(void) { //在堆中申请100个char类型的内存 char *str = new char[100]; //拷贝Hello C++字符串到分配的堆中的内存中 strcpy(str, "Hello imooc"); //打印字符串 cout<<str<<endl; //释放内存 delete []str; str=NULL; return 0; }查看全部
-
现在有一个数组,定义一个方法getMax(),利用函数的重载,分别实现: 1、随意取出数组中的两个元素,传到方法getMax()中,可以返回较大的一个元素。 2、将整个数组传到方法getMax()中,可以返回数组中最大的一个元素。 #include <iostream> using namespace std; /** *函数功能:返回a和b的最大值 *a和b是两个整数 */ int getMax(int a, int b) { return a > b ? a : b; } /** * 函数功能:返回数组中的最大值 * arr:整型数组 * count:数组长度 * 该函数是对上面函数的重载 */ int getMax(int *arr,int count) { //定义一个变量并获取数组的第一个元素 int temp=arr[0]; for(int i = 1; i < count; i++) { //比较变量与下一个元素的大小 if(temp<arr[i]) { //如果数组中的元素比maxNum大,则获取数组中的值 temp=arr[i]; } } return temp; } int main(void) { //定义int数组并初始化 int numArr[3] = {3, 8, 6}; //自动调用int getMax(int a, int b) cout << getMax(6,3) << endl; //自动调用返回数组中最大值的函数返回数组中的最大值 cout << getMax(numArr,3) << endl; return 0; }查看全部
-
内存管理小tip查看全部
-
//const #include <iostream> using namespace std; int main(void) { const int count=3; int const *p = &count; //打印count次字符串Hello C++ for(int i = 0; i < *p; i++) { cout << "Hello imooc" << endl; } return 0; }查看全部
-
指针指向const修饰的变量时 , 应该是const int const *p = &a ; (被引用者的权限应该大于或等于引用者)查看全部
-
有默认值的函数放在最右端 声明写默认值,定义不写默认值 内联函数不能是递归函数,编译器不承认查看全部
-
有默认值的函数放在最右端查看全部
-
const(控制变量是否可以变化) const int x=3;(则此时x为常量,不可进行再赋值) const与指针类型 const int *p=NULL; int const *p=NULL;(两种写法完全等价) int *const p=NULL; const int *const p=NULL; int const *const p=NULL;(这两种写法也是完全等价的) int x=3; const int *p=&x; *p=4(错误,因为const指定的为*p);p=&y;(正确) int x=3; const int *const p=&x; p=&y(错误,因为const指向的为p,只能为x的地址) const与引用 int x=3;const int &y=x; y=10(错误,y通过const限定只能为x的别名,值为3) 总结: const int x=3;int *y=&x;(这种写法是错误的因为x本身定义为const,在用一个可变的指针指向,那么就有用指针改变x值得风险,这是系统所不允许的); int x=3; const int *y=&x;(正确,这样保证了指针对x只有可读性,而没有可写性)查看全部
-
const 定义常量 用法: int *const p=&x; 不固定*p的值,固定p的指向 int const *p=&x; 固定*p的值,不固定p指向 int const * const p=&x==const int * const p=&x; con int x=3; int *y=&x;这是错误的,不能大范围指向小范围 正确写法:int x=3;const int*y=&x;固定的指针指向X 这是我自己领悟到的,有错的地方多多包涵!查看全部
-
O(∩_∩)O哈哈~查看全部
-
const查看全部
-
截图成了没查看全部
-
函数重载查看全部
-
引用 int a; int &a1 = a;查看全部
-
声明写默认值,定义不写默认值查看全部
举报
0/150
提交
取消