-
单个内存的申请: #include <iostream> using namespace std; int main() { int *p=new int; if ( p==NULL ) { cout << "申请错误!" << endl; return 0; } *p = 20; cout << *p << endl; delete p; p = NULL; return 0; } 块内存申请: #include <iostream> using namespace std; int main() { int *p=new int[1000]; if ( p==NULL ) { cout << "申请错误!" << endl; return 0; } p[0] = 10; p[1] = 20; cout << p[0] << " " << p[1] << endl; delete []p; p = NULL; return 0; }查看全部
-
#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(3 , 4 ) << endl; //自动调用返回数组中最大值的函数返回数组中的最大值 cout << getMax(numArr, 3) << endl; return 0; }查看全部
-
222查看全部
-
111查看全部
-
123查看全部
-
//const #include <iostream> using namespace std; int test(int a = 10) { cout << a << endl; return 0; } int getMax(int a, int b, int c) { int temp = a; if (temp <= b) { temp = b; } if (temp <= c) { temp = c; } return temp; } double getMax(double a, double b) { double temp = a; if (temp <= b) { temp = b; } return temp; } inline int getMax1(int a, int b, int c) { int temp = a; if (temp <= b) { temp = b; } if (temp <= c) { temp = c; } return temp; } int main(void) { cout << "int max function:" << getMax(1, 4, 7) << endl; cout << "int max function:" << getMax1(1, 4, 7) << endl; cout << "double max function:" << getMax(1.6, 40.5) << endl; return 0; }查看全部
-
#include <iostream> using namespace std; int varRef(void) { int a = 10; int &b = a; b = 20; cout << a << endl; return a; } int structRef(void) { typedef struct { int x; int y; } Coor; Coor c1; Coor &c = c1; c.x = 10; c.y = 20; cout << c1.x << "; " << c1.y << endl; return 0; } int pointerRef(void) { int a = 10; int *b = &a; int *&c = b; *c = 20; cout << a << endl; return 0; } int functionParaRef(int &a, int &b) { cout << "a:" << a << "; b:" << b << endl; int c = 0; c = a; a = b; b = c; cout << "a1:" << a << "; b1:" << b << endl; return 0; } int main(void) { varRef(); structRef(); pointerRef(); int a = 1; int b = 2; functionParaRef(a, b); return 0; }查看全部
-
关于内存的申请和释放的内容总结查看全部
-
内存的申请和释放查看全部
-
内存的申请和释放查看全部
-
内联函数查看全部
-
参数默认值查看全部
-
声明参数可以写默认值,定义不写查看全部
-
函数参数默认值查看全部
-
指针类型的引用查看全部
举报
0/150
提交
取消