-
注意: 申请内存需要判断是否成功,释放内存需要设空指针。 new与delete配套使用。查看全部
-
getMax(int x,int y,int z) --> getMax_int_int_int getMax(double x,double y) --> getMax_double_double查看全部
-
const int x=3;int *y=&x;(错) 这里x是不可变的,而*y是可变的,所以这样定义是错误的。查看全部
-
指针引用: *&别名 = 指针查看全部
-
#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; }查看全部
-
有默认参数值的参数必须在参数表的最右端查看全部
-
指针指向const时 应用const*p查看全部
-
#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 maxNum=arr[0]; for(int i = 1; i < count; i++) { //比较变量与下一个元素的大小 if(arr[i]> maxNum) { //如果数组中的元素比maxNum大,则获取数组中的值 maxNum=arr[i]; } } return maxNum; } int main(void) { //定义int数组并初始化 int numArr[3] = {3, 8, 6}; //自动调用int getMax(int a, int b) cout << getMax(numArr[0], numArr[2]) << endl; //自动调用返回数组中最大值的函数返回数组中的最大值 cout << getMax(numArr,3) << endl; return 0; }查看全部
-
const *p 修饰 变量*p *const p 修饰 地址p查看全部
-
const可以放在类型前面或后面查看全部
-
引用必须被初始化 int &b = a;查看全部
-
nclude <iostream> using namespace std; int main(void) { //定义常量count const int count = 3; const int *p = count; int count="Hello c++"; //打印count次字符串Hello C++ cout<<"Hello c++"<<endl; int i=0; count ismax; int count=" hello imooc"; void fun() for(int i = 0; i < count; i++) { cout << "Hello imooc" << endl; } return 0; }查看全部
-
#include <iostream> using namespace std; int main(void) { int x = 3; //定义引用,y是x的引用 int&y=x; //打印x和y的值 { < cout<<x<<","<y<<endl; } //修改y的值 y = 10; //再次打印x和y的值 { < cout<<x<<","<y<<endl; } return 0; }查看全部
-
C语言和C++各自内存管理方式查看全部
-
#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<<"Hello imooc"<<endl; //释放内存 delete []str; str=NULL; return 0; }查看全部
举报
0/150
提交
取消