c++中使用数组的有效赋值
valid assign for use array in C++
我想使用动态内存,但我不希望每次使用加上指针到特定位置。我总是用像
*(myPointer+position)
所以我想这样使用但我不知道这是否有效。
struct profile{
double x;
double y;
int intencity;
};
profile *asdf;
profile asdfe;
asdf=new profile[1024];
///here is my problem
asdfe=asdf[myposition];
我的问题是这个{asdfe=asdf[myposition];}是否有效?
myPointer[position]
等价于*(myPointer+position)
asdfe=asdf[myposition];
等价于
asdfe=*(asdf + myposition);
可以使用
asdfe=*asdf[mypostion]
尽管您可能希望确保asdf[myposition]
首先是有效的。我可以建议创建一个测试应用程序来测试这个概念吗?指针的行为通常类似于数组,因此大多数编译器允许您以这种方式使用它们。
为了证明这个概念,你也可以使用
asdf[5] ->x = 7.3; //I made that up I don't have much context
asdf[5] ->y = 9.2;//That one too
asdf[5] ->intencity = 9001; //It's over 9000! Also it's spelled intensity.
asdfe=*asdf[5];
if(asdfe.x == 7.3)
cout << "It worked!" << endl;
应该是这样的。据我所知。就像我说的,在一个测试应用程序上尝试一下,你可以自由地玩一些你不确定的东西。
相关文章:
- 为"adjacent"变量赋值时出现问题
- C++中的赋值发生,尽管右侧出现异常
- 用C++中的sscanf赋值
- 为std::string的某个索引赋值
- 重载Singly Linked List中的赋值运算符
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- gtest_使用setargpointee在函数中赋值
- 非常量变量只读位置的赋值
- 使用赋值运算符重载从类中返回jobject
- C++数据文件、数组和计算赋值
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 全局作用域中函数指针的赋值
- 错误:在为指针赋值时,void值没有被忽略
- 在C++03中,在未赋值的上下文中使用表示非静态数据成员的id表达式有效吗
- c++中使用数组的有效赋值
- 函数指针赋值在C中有效,但在C++中无效
- 在有效的c++中使用赋值
- 有效的c++项目11处理对self的赋值
- 在c++中这是一个有效的赋值吗?
- 为什么对基类的赋值有效,但对派生类的赋值是编译错误