首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以这样创建一个新地址吗?

我可以这样创建一个新地址吗?
EN

Ethereum用户
提问于 2018-09-16 14:41:15
回答 1查看 92关注 0票数 2

我正忙着做一个项目。所以我在另一个结构中有一个结构数组。在这种情况下,例如牛中的CattleHealth数组。在我的RecordHealth函数中,我使用牛结构的地址并添加当前列表的当前长度来创建唯一的地址。这样做对吗?

代码语言:javascript
复制
  contract WagyuRecordContract
    {
        address owner;

        struct Cattle
        {
            address RFID;
            string Name;
            uint256 Weight;
            string Gender;
            string Colour;
            string Breed;
            uint Age; 
            uint DOB;
            string Location;
            bool Parent;
            string SireName;
            string DamName;
            bool Active;
            bool ForSale;
            CattleHealth[] HealthRecord;
            CattleGrowth[] GrowthRecord;
            CattleMovements[] MovementsRecord;
            Facility SlaughterDetails;
            Meat[] DistributionDetails
        }


        struct CattleGrowth
        {
            uint DateRecorded;
            uint256 FoodIntake;
            uint256 Growth;
        }


        mapping (address => Cattle) public cattle;
        mapping (address=> CattleGrowth) public growth;           

        modifier Owner() 
        {
            require(msg.sender == owner);
            _;
        }


        function RecordHealth(address rfid, string _bodyCond, string _healthStat, uint256, string _med) Owner public
        {
            health[rfid+cattle[rfid].HealthRecord.length].DateRecorded = now;
            health[rfid+cattle[rfid].HealthRecord.length].BodyCondition = _bodyCond;
            health[rfid+cattle[rfid].HealthRecord.length].HealthStatus = _healthStat;
            health[rfid+cattle[rfid].HealthRecord.length].Medication = _med;
            cattle[rfid].HealthRecord.push(health[rfid+cattle[rfid].HealthRecord.length]);
        }
}

编辑:我的伙伴在没有使用映射的情况下使用了下面的方法。我不明白这是怎么回事。-1是干什么用的?它为什么对牛来说是独一无二的呢?

代码语言:javascript
复制
   function RecordHealth(address rfid, string _bodyCond, string _healthStat, string _med) Owner public
    {
       cattle[rfid].HealthRecord.push(now, _bodyCond, _healthStat, _med)-1;
    }
EN

回答 1

Ethereum用户

回答已采纳

发布于 2018-09-16 16:14:03

不是。虽然在C语言或类似语言中指针算法是正常的规则,但在稳健性中,它不能保证工作,无论是对于最终的包装和最终的循环不对齐。因此,强烈建议不要这样做。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/58846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档