如何使用'new'运算符声明二维数组?我的书说:
int (*p)[4];
p=new[3][4];
Run Code Online (Sandbox Code Playgroud)
但这对我没有意义.p是一个指向4个整数数组的指针,那怎么能指向一个二维数组呢?
如果我声明一个包含10个元素的字符串,如下所示:
char s[10];
Run Code Online (Sandbox Code Playgroud)
那么'\ 0'到底是占据第10位还是第11位?基本上我的问题是我们在字符串中少了1个元素吗?
如果我使用strlen()函数来查找此字符串的长度,返回值是否包含null?即如果字符串是"男孩",函数会给我3或4吗?
class sample
{
private:
int radius;
float x,y;
public:
circle()
{
}
circle(int rr;float xx;float yy)
{
radius=rr;
x=xx;
y=yy;
}
circle operator =(circle& c)
{
cout << endl<<"Assignment operator invoked";
radius=c.radius;
x=c.x;
y=c.y;
return circle(radius,x,y);
}
}
int main()
{
circle c1(10,2.5,2.5);
circle c1,c4;
c4=c2=c1;
}
Run Code Online (Sandbox Code Playgroud)
在重载'='函数中的语句
radius=c.radius;
x=c.x;
y=c.y;
Run Code Online (Sandbox Code Playgroud)
本身使所有c2的数据成员都等于c1,那么为什么需要返回?类似地,在c1 = c2 + c3中,使用重载+运算符添加c2和c3,并将值返回到c1,但不会变为c1 =,因此我们不应该使用another =运算符来分配总和c2和c3到c1?我糊涂了.
c++ return operator-overloading inner-classes assignment-operator
我在这个程序的两个不同的地方得到了相同的错误,它应该是一个1d,一个2d和一个3d数组并存储值并同时显示它们.错误:下标需要数组或指针类型/表达式必须具有指针到对象类型,错误是表达式c [r] [c] [depth]
#include<iostream>
using namespace std;
#define ROW 5
#define COL 5
#define DEPTH 5
int main()
{
int *a; // 1d array
a=new int [COL];
int (*b) [COL]; //2d array
b=new int [ROW][COL];
int (*c)[ROW][COL];
c=new int [ROW][COL][DEPTH]; // 3d array
//---------------------------------------------------------------------------------
// storing values in the arrays:
for(int i=0;i<COL;i++)
{
a[i]=i+2;
cout << a[i];
}
// 2d array
for(int r=0;r<ROW;r++)
{
for(int c=0;c<COL;c++)
{
b[r][c]=r+c+2;
cout << b[r][c];
}
}
// 3d array
for(int r=0;r<ROW;r++)
{ …Run Code Online (Sandbox Code Playgroud) c++ arrays new-operator multidimensional-array dynamic-arrays