0

I want to update value of a variable at run time, present in project configuration as per some condition. But currently I am getting this error: error: lvalue required as left operand of assignment

Actual code:

#include "contiki.h"
#include <stdio.h> /* For printf() */
/*---------------------------------------------------------------------------*/

PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);

static void update_project_conf_value(void)
{
    printf("Original Value: %d\n",TEST_VALUE);
    TEST_VALUE = 0;
    printf("After update: %d\n",TEST_VALUE);
}

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
    PROCESS_BEGIN();
    update_project_conf_value();
    PROCESS_END();
}
/*---------------------------------------------------------------------------*/

Project configuration:


#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_

#define TEST_VALUE 1
/*---------------------------------------------------------------------------*/
#endif /* PROJECT_CONF_H_ */
/*---------------------------------------------------------------------------*/

Note: I want to update it in one of file as per some condition and then use the updated value in a different file.

  • `TEST_VALUE` isn't a variable that can be updated – Chris Turner Sep 26 '19 at 16:30
  • See I donno about contiki, but as far as I know about `C`, macros are not updated like that. Macros are preprocessed. `TEST_VALUE` is not a variable. Nothing like `TEST_VALUE` would even exist at run time, it would be replaced by 1 everywhere. – Mihir Luthra Sep 26 '19 at 16:31
  • In case you need macro values based on conditions, you can use `#if`, `#elif` to give that macro a value. – Mihir Luthra Sep 26 '19 at 16:32
  • Thanks @Mihir, Is there any possible way by which I can update its value as per some condition at runtime? – SUBHANSHU SAHU Sep 26 '19 at 16:38
  • @SUBHANSHUSAHU , you should understand what macros are at first place. Assume you have a code in which you find a string and just replace it. Thats what is done. At run time TES_VALUE doesn't even exist. The replacement is the first thing to take place, even before compiling. – Mihir Luthra Sep 26 '19 at 16:45
  • Note that if the code that's compiled varies depending on the setting of `TEST_VALUE`, then changing it (by any means) at run time will mean that the program needs to be recompiled too. – Jonathan Leffler Sep 26 '19 at 17:12

1 Answers1

1

First off, TEST_VALUE is a macro. You can read it but you can not write to it. It will also disappear at runtime.

What you really want is a global variable.

In the header put something like this:

#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_

int g_TEST_VALUE; // Declaration

/*---------------------------------------------------------------------------*/
#endif /* PROJECT_CONF_H_ */
/*---------------------------------------------------------------------------*/

in your source put something like this:

#include "contiki.h"
#include <stdio.h> /* For printf() */
/*---------------------------------------------------------------------------*/

extern int g_TEST_VALUE = 1; // Definition

PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);

static void update_project_conf_value(void)
{
    printf("Original Value: %d\n",TEST_VALUE);
    g_TEST_VALUE = 0;
    printf("After update: %d\n",TEST_VALUE);
}

/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
    PROCESS_BEGIN();
    update_project_conf_value();
    PROCESS_END();
}
/*---------------------------------------------------------------------------*/
John
  • 1,012
  • 14
  • 22
  • The global variable is updated for current file but I want the value of variable to be updated in current file and then use the updated value in different file. I tried with following code: ```int local_variable; if (a==5) {local_variable=5;} else {local_variable=0;} extern int global_TEST_VALUE = local_variable;``` But I am getting this error message: ```error: initializer element is not constant``` – SUBHANSHU SAHU Sep 26 '19 at 18:37
  • @SUBHANSHUSAHU ah, so each other time you use it (except for the one time at the beginning `extern int g_TEST_VALUE = 1` you should use it as `g_TEST_VALUE = local_variable` – John Sep 26 '19 at 18:46
  • in this case, if I print the value of ```g_TEST_VALUE``` from a different file, the value that is assigned while the definition is displayed i.e.```1``` because of ```extern int g_TEST_VALUE = 1;``` instead of ```0``` that should be because of ```g_TEST_VALUE = local_variable``` – SUBHANSHU SAHU Sep 26 '19 at 19:37
  • @SUBHANSHUSAHU Indeed that is correct. You get the value depending on the file you compile and run. At this point I'm very unsure what your end goal is. – John Sep 26 '19 at 19:41
  • Since I have updated the value of ```g_TEST_VALUE``` equal to ```0``` in above code. I want this updated value of ```g_TEST_VALUE``` in different file instead of the definition value – SUBHANSHU SAHU Sep 26 '19 at 20:01
  • @SUBHANSHUSAHU That should work then. You first run the code which updates it to 0 followed by the code which uses it. – John Sep 26 '19 at 20:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200032/discussion-between-john-and-subhanshu-sahu). – John Sep 26 '19 at 20:05