Using #define value in another #define with dynamically loaded first #define value

String id = "1584";
#define THINGNAME_1  id
#define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"

void setup() {
   Serial.begin(115200);
}

void loop() {
  Serial.println(THINGNAME_1);
  Serial.println(TOPICNAME_1);
  delay(1000);
}

for this program, i get the following error

F:\Arduino\sketch_jul16a-define\sketch_jul16a-define.ino: In function 'void loop()':
F:\Arduino\sketch_jul16a-define\sketch_jul16a-define.ino:4:22: error: expected ')' before 'id'
    4 | #define THINGNAME_1  id
      |                      ^~
F:\Arduino\sketch_jul16a-define\sketch_jul16a-define.ino:5:37: note: in expansion of macro 'THINGNAME_1'
    5 | #define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"
      |                                     ^~~~~~~~~~~
F:\Arduino\sketch_jul16a-define\sketch_jul16a-define.ino:13:18: note: in expansion of macro 'TOPICNAME_1'
   13 |   Serial.println(TOPICNAME_1);
      |                  ^~~~~~~~~~~
F:\Arduino\sketch_jul16a-define\sketch_jul16a-define.ino:13:17: note: to match this '('
   13 |   Serial.println(TOPICNAME_1);
      |                 ^

exit status 1

Compilation error: expected ')' before 'id'

if i use direct "1584" instead of id in THINGNAME_1, it works fine.
only during pulling time, it makes this error. how to solve this??

#define works with text substitution, not program variables.

See Replacing text macros - cppreference.com

But this program pulls a value from a variable

String id = "1584";
#define THINGNAME_1  id
//#define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"

void setup() {
   Serial.begin(115200);
}

void loop() {
  Serial.println(THINGNAME_1);
  //Serial.println(TOPICNAME_1);
  delay(1000);
}

So, some minute configuration is there to do, i feel

I have no idea what that does, and recommend to avoid Strings on Arduino, as they often cause program malfunctions and crashes, due to poor memory management.

What do you really want to do?

Without testing,I suspect the issue is using a String.
Try it with a char array instead.

Strings are not strings.

The pre-processor knows nothing about c/c++ variables. It works only with strng tokens. This is what your code reduces to, after the pre-processor runs:

String id = "1584";
#define THINGNAME_1  id   << Note this is unchanged, because the pp does not recognize id as a variable
//#define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"

void setup() {
   Serial.begin(115200);
}

void loop() {
  Serial.println(id);   << Note that THINGNAME_1 is replaced by id
  //Serial.println(TOPICNAME_1);
  delay(1000);
}

[/quote]

Yes, thanks. I should have seen that.

This makes
TOPICNAME_1 = "$aws/things/" id "/shadow/update"
with an illegal mix of string literals and letters. No string concatenation can be made with this construct.

If you want to use the actual content of the variable id then you have to concatenate the strings in code. Otherwise you can write

#define THINGNAME_1  "1584"
String id = THINGNAME_1;
#define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"

don't use a String

this should work

#define id "1584"
#define THINGNAME_1  id
#define TOPICNAME_1  "$aws/things/" THINGNAME_1 "/shadow/update"

void setup() {
  Serial.begin(115200);
  Serial.println(THINGNAME_1);
  Serial.println(TOPICNAME_1);}

void loop() {}

#define THINGNAME_1 id is changing. id value is dynamically loaded by String id.
that's the whole issue.

first #define is pulling a value from a variable but second #define is not pulling the value from dynamically loaded first #define which is linked with a variable.

If i do static load the first #define, the same second #define pulls a value from that first #define.

it works :heart_eyes:

But is wasteful on memory.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.