pragma solidity ^0.4.24;

contract online_shopping{

    struct item {
        uint price;
        string name;
        //string seller;

    struct soldItems{
        address customer;
        uint tprice;

    mapping (address=>uint) sold;
    uint public number;
    uint public totalPrice;
    item  [] public items;
    //soldItems [] public si;
    uint public len;
    address public owner;
    constructor () public{

    modifier  onlyOwner() {


    function  addItems(uint _price,string _name) public onlyOwner  {

            price : _price,
            name : _name
           // seller : _seller

    function () payable public{}

    function  sellItems (uint _number,string _name)public payable returns(bool)

        bool f;
        uint p;

        for(uint i=0;i<items.length;i++)
            if (keccak256(abi.encodePacked(items[i].name))==keccak256(abi.encodePacked(_name)))
            { f= true;p=i;}
          totalPrice= items[p].price*number; 

            return true;
            return false;


I tried to read the length separately, but it is showing zero length.

  • 1
    items.length should tell you the length of the items array, but that's what your code is already doing. What specific problem are you having?
    – user19510
    Aug 20, 2019 at 22:33
  • I am not able to access the name feature from the item structure at the specific index.
    – Akp101
    Aug 21, 2019 at 11:28
  • I'm not sure what you mean. Are you seeing an error of some sort? Or is the result of running your code not what you expected?
    – user19510
    Aug 21, 2019 at 17:11
  • Yeah.. Actually when I run my code to display the total amount. It's showing zero. So I thought maybe the string equality is not working properly
    – Akp101
    Aug 22, 2019 at 3:50
  • I have no idea what "the total amount" means or why it being zero suggests a problem. I'm going to vote to close this question.
    – user19510
    Aug 22, 2019 at 3:55

1 Answer 1


You can only get the length of arrays.

It is not possible to get the length of structs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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