2

I am busy with a project. So I have an array of a struct within another struct. In this case for example array of CattleHealth in Cattle. In my RecordHealth function, I am taking the address of the Cattle struct and adding the current length of its current list to create a unique address. Is this a correct way to do it?

  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]);
        }
}

Edit: My Buddy used this method below without using a mapping. I don't understand how this works. What is the -1 for? How is it unique to the cattle?

   function RecordHealth(address rfid, string _bodyCond, string _healthStat, string _med) Owner public
    {
       cattle[rfid].HealthRecord.push(now, _bodyCond, _healthStat, _med)-1;
    }
4
  • Does rfid+cattle[rfid].HealthRecord.length even compile? You are trying to add address + uint. Looks like you by the least need to replace it with address(uint(rfid)+cattle[rfid].HealthRecord.length). Commented Sep 16, 2018 at 14:45
  • BTW, you're gonna have a hard time tracking those new addresses that you've added to the health mapping. Commented Sep 16, 2018 at 14:50
  • And last note: It looks like you've posted a lot more than what's actually needed in order to answer your question, which makes it very hard (and annoying) to read through. I recommend that you get rid of everything except for what is absolutely necessary, and after you receive an answer, find a way to embed it into your original code. Commented Sep 16, 2018 at 14:52
  • Noted, I edited it and also added a snippet of what someone else did which I do not understand. Sorry new to this game.
    – Eli2099
    Commented Sep 16, 2018 at 15:34

1 Answer 1

0

Negative. While in C or similar languages the pointer arithmetic is the normal rule, in Solidity it is not guaranteed to work, both for eventual packing and for eventual cyclic misalignment. So it is strongly advised not to do so.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.