# updating variable inside and outside struct, different results, why?

With this, `bool p` does not update,

``````contract b {

bool p;

mapping(uint => bool) public t;

function setValue() {
bool b = t[1];
b = true;
}
function checkValue() returns (bool) {
return t[1];
}
}
``````

but with this, it does, why?

``````contract b {

struct Info {
bool p;

}
mapping(uint => Info) public t;

function setValue() {
Info b = t[1];
b.p = true;
}
function checkValue() returns (bool) {
return t[1].p;
}
}
``````
• It's an unfortunate aspect of the Solidity language. `Info b = t[1];` is implicitly the same as `Info storage b = t[1];`, so `b` is actually a pointer to the struct in storage. – Jesse Busman Dec 30 '18 at 14:45

The difference is that bool is a value type while struct is a reference type.

In your first snippet, your line `bool b = t[1];` is retrieving a copy of t[1], because t[1] is a boolean primitive. It assigns it to a local variable b. Modifying `b` has no consequence on the values stored in `t`.

In your second snippet, your line `Info b = t[1];` retrieves a reference to t[1], because t[1] is a structure. The reference is assigns to b, and writing to `b.[field]` effectively modifies the fields of the structure.

The difference between value type and reference type is fundamental in computer languages, I suggest you read more about it here: https://en.wikipedia.org/wiki/Value_type_and_reference_type

Solidity has the concept of reference types and there are several of them such as dynamic arrays, fixed arrays, mappings and structs. You can read more about that here: https://solidity.readthedocs.io/en/v0.4.24/types.html

A struct is like a class of sorts where we have to create a new instance of it to use it.

This struct will have a couple of different fields that will describe its purpose for example:

``````contract Crowdfunding {
struct Request {
string description;
uint value;
bool complete;
}
}
``````

Now a struct behaves differently than variables do, for example:

``````contract Crowdfunding {
struct Request {
string description;
uint value;
bool complete;
}

uint public minimumContribution;

function Crowdfunding(uint minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
}
``````

Below the struct I have variables I created that I can access in a function, but a struct does not contain variables it contains reference types. So if you want to use the `Request` struct, then you have to create a new variable and say it is a type of `Request`.

So the above does not create an instance of a `Request` it just creates the idea of it or the type.

To actually create a request and have it stored inside the contract I will make an array that specifically holds variables of type request.

I am going to make a new array and specify its type is being request.

``````contract Crowdfunding {
struct Request {
string description;
uint value;
bool complete;
}

Request[] public requests;
uint public minimumContribution;

function Crowdfunding(uint minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
``````

Notice I am using capital R because I used a capital R to define the struct. By convention we use capital for first letter when defining a struct which is dissimilar to how we do our other type definitions.

This variable of type `Request` is given the name `requests` and now anywhere in my contract I can make use of the `requests` array.

I could say `requests.push()` and push in a new request or give me the new requests in there with request index of zero like so: `requests[0]`.