3

I dont know if this is possible, but help give suggestions.

I have a variableunsigned char Var_2_insert; and I have an array const unsigned char insert_here[4];.

This variable is initialized at run time say var_2_insert = 225;. The idea is that index 3 in the array should have the value of var_2_insert always. So I want to try something like

insert_here[4] = { 0x00, 0x02, 0x03, Var_2_insert};

So that any time I try to read the array insert here, It will have the present value of var_2_insert. Now I know if I go like this:

#define Var_2_insert 225

This will go well, but since this variable has to be updated at runtime, I tried

#define _VAR Var_2_insert
insert_here[4] = { 0x00, 0x02, 0x03, _VAR};

But does not work. So how can i go about this? I hope my question is clear enough.

Thanks.

5
  • Question not clear. With this const unsigned char insert_here[4]; you said contents of insert_here should be constant and you have another condition "So that any time I try to read the array insert here, It will have the present value of var_2_insert." . Can you explain please Commented Feb 21, 2013 at 9:48
  • Any reason why this is tagged embedded? Commented Feb 21, 2013 at 13:26
  • @Toms I have been corrected with that already. It would no longer be defined as a constant. Commented Feb 21, 2013 at 14:21
  • @Lundin. I am writing for an embedded application, so I though there might be some rules that may apply in this case. All the same, thanks for your comment. Commented Feb 21, 2013 at 14:22
  • @PaulA. No there are no special rules, unless you meant the "insert here" variable to be a constant located in NVM, which may be changed in runtime from a NMV programming algorithm. In that case it must be declared as volatile and your question needs to be rewritten completely. Commented Feb 21, 2013 at 15:44

6 Answers 6

5

What you can do is have Var_2_insert a pointer to the 3rd index in the array:

unsigned char insert_here[4]; // since it's updating it shouldn't be const
unsigned char *Var_2_insert = &(insert_here[3]);
//you just need to update the use of var_2_insert to dereference..
//  var_2_insert = 225 // <-- before
   *var_2_insert = 225 // <-- after
Sign up to request clarification or add additional context in comments.

Comments

3

If the array elements change at run time, then they are not really const. "#define" is same directly typing hard coded value except that you let the preprocessor do the work among other benefits. But this won't help dynamically change the value as you want here.

Declare an array as an array of pointers and refer to that instead of using the original array like this:

int var = 225;
int insert[4] = { 0x00, 0x02, 0x03, 255};
int *a[4] = { &insert[0], &insert[1], &insert[2], &var};

Use this array of pointer from now on which will have the updated value of var.

Comments

2

why declare it as a constant if it is not?

unsigned char insert_here[4] = { 0x00, 0x02, 0x03, 0x00}; /* remove const */
insert_here[3] = Var_2_insert;

Comments

2

Array initializers define the array contents at compile time, so you need to know the values to define with at compile time. But one of the values is a variable, which is defined at runtime.

In this case, you need to move array initialization to runtime:

unsigned char insert_here[4] = { 0x00, 0x02, 0x03};
insert_here[3] = Var_2_insert;

But in this case you are modifying the array after its declaration, so you need to remove the const from the array definition.

6 Comments

This is a one time solution. he ask for a way insert_here[3] is equal to Var_2_insert all the time.
@Roee It is, maybe for the tiny speck of time between execution of those two lines the values are different. I don't think that is a problem.
but what would happen if Var_2_insert will change it's value ?
@Roee C is a pass-by-value language, not pass-by-reference. The second line takes the value in Var_2_insert and copies it to the insert_here[3] variable. Subsequent changes to Var_2_insert are not reflected in insert_here array.
Exactly! but that's not what the question is asking. he WANT a way that changes in Var_2_change will be reflected in the array. which can be achieved if Var_2_change is a pointer to that entity in the array. Anyway your answer doesn't answer the question...
|
2

Hum... I don't understand why do you need a #define. If you need to modify your var at runtime, use a variable, not a define. the first way works fine :

char Var_2_insert = 225;
const unsigned char insert_here[4] = { 0x00, 0x02, 0x03, Var_2_insert};


"#define _VAR Var_2_insert" And this will just replace all "_VAR" by "Var_2_insert" at compile time, so you can just put directly the var.


EDIT : Ok, So you want that at any time, insert_here[3] has the value of Var_2_insert. It's more complicated and introduce some side effects that can make hard bugs, but if you know what you do, you can do it with pointers like that:

const unsigned char insert_here[4] = { 0x00, 0x02, 0x03, 0};
unsigned char * Var_2_insert2 = (char*) &insert_here[3];

And then, if you want use it as a variable, a #define is the only (but bad) solution:

#define _VAR (*Var_2_insert2)

And you keep your const on the table but it just tell rest of your code that it is not possible to modify it, and not it is constant.

1 Comment

it will not satisfy the condition "So that any time I try to read the array insert here, It will have the present value of var_2_insert". During intilaization only the same value
2

Further to @Roee Gavirel's answer, if you were to use C++ rather than C, you can clean up the syntax a bit by using a reference variable:

unsigned char insert_here[4] ;
unsigned char& Var_2_insert = insert_here[3];

var_2_insert = 225 // modifies insert_here[3]

Apart from not needing the dereference operator, the difference here is that the reference can be initialised but not assigned, so it will always refer to insert_here[3], whereas a pointer variable can be reassigned (unless you declared it unsigned char* const Var_2_insert2, but you'd still need to dereference).

Like many C++ features this instance carries zero overhead over the equivalent C code.

1 Comment

Actually, in the standard, it's not specified how references are implemented. Even if most compiler should interpret them as aliases, there is no waranties there won't be any overhead

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.