Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Variable modifiers are fixed at compile time for each variable. You may have to explain the context of what you are trying to do, but perhaps this will suit your needs?</p> <pre><code>struct s { int* const B_ID; }; int main (void) { int n = 5; s d = {&amp;n}; int* value = d.B_ID; // ok // d.B_ID = &amp;n; // error return 0; } </code></pre> <p>Since you are using C++ I would recommend:</p> <pre><code>class s { public: int* const B_ID; s (int* id) : B_ID (id) { } }; void main (void) { int n = 5; s my_s_variable = s(&amp;n); int* value = my_s_variable.B_ID; // ok //my_s_variable.B_ID = &amp;n; // error return 0; } </code></pre> <p><code>Ramiz Toma</code>: <strong>well i need way to do it using the s.B_ID=something</strong></p> <p>In C/C++ type modifiers (like const) are declared at run time for a given type and cannot be changed at run time. This means that if a variable is declared <code>const</code> it can never be assigned to using the assignment operator. It will only be assigned a value when it is constructed.</p> <p>This is not a problem however because you can always get around this by proper design.</p> <p>If you say you need to use assignment, I assume that this is because you create the struct before you know what the value of the variable will be. If this is the case then you simply need to move the struct declaration till after you know the value.</p> <p>For example</p> <pre><code>s d; //variable declaration //calculate B_ID //... int* n = 5; //... d.B_ID = &amp;n; </code></pre> <p>This will not work, because if you want b.D_ID to be 'un assignable' it will always be so. You will need to refactor your code similarly to:</p> <pre><code>//calculate B_ID //... int* n = 5; //... s d (&amp;n); //good </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload