arrays - 为什么 char 数组在动态结构数组中不起作用?
问题描述
我试图了解 C++ 中的结构。我想制作一个动态结构数组,用一些值对其进行初始化,然后在屏幕上查看这些值。为什么它在使用字符串时有效,而在使用 char 数组时无效。
#include <iostream>
struct batonik
{
char name[20];
double weight;
int calories;
};
int main()
{
batonik *ps = new batonik[3]
{
{ "snickers",2.2,350 },
{ "mars",2.6,420 },
{ "bounty",2.1,210 }
};
for (int i = 0; i < 3; i++)
{
std::cout << "Nazwa batonika : " << (*(ps+i)).name << std::endl;
std::cout << "Waga batonika : " << (ps+i)->weight << std::endl;
std::cout << "Ilosc kalorii : " << (ps+i)->calories<< std::endl;
std::cout << i;
}
delete [] ps;
return 0;
}
解决方案
推荐阅读
- php - MYSQL 只限制左表的结果,但显示右表的所有结果
- kotlin - 如何将 DropdownMenu 移动到 Jepack Compose 中的首选位置
- javascript - 当我使用 firebase 部署我的网站时,我的联系表不起作用,但当我使用本地主机时它会起作用。我的第一个网站,所以不知道该怎么做
- sql - 在字段中查找重复值会导致多行相同的值
- python-3.x - 使用 Pythin 实现证书链验证器
- sql - PSQL 递归添加查询
- php - 创建 PHP yii 框架代码文档
- javascript - 如何在单击按钮中获取动态div的所有索引显示更多jquery
- webpack - 为什么 webpack 缓存卡住了?
- c# - FFMPEG:使用图像序列作为不同的输入