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

**URL:** <https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521>\
**Category:** Programming\
**Created:** [July 16, 2023, 3:42am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521 "2023-07-16T03:42:34Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![moorthypnt](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@moorthypnt](https://forum.arduino.cc/u/moorthypnt)\
**Post date:** [July 16, 2023, 3:42am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/1 "2023-07-16T03:42:35Z")

</div>

```arduino
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

```arduino
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??

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 16, 2023, 3:48am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/2 "2023-07-16T03:48:23Z")

</div>

#define works with text substitution, not program variables.

See [Replacing text macros - cppreference.com](https://en.cppreference.com/w/cpp/preprocessor/replace)

---

<div class="post-metadata">

**Author:** ![moorthypnt](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@moorthypnt](https://forum.arduino.cc/u/moorthypnt)\
**Post date:** [July 16, 2023, 3:52am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/3 "2023-07-16T03:52:27Z")

</div>

But this program pulls a value from a variable

```arduino
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

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 16, 2023, 3:58am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/4 "2023-07-16T03:58:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lastchancename](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lastchancename/32/215426_2.png) [@lastchancename](https://forum.arduino.cc/u/lastchancename)\
**Post date:** [July 16, 2023, 3:58am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/5 "2023-07-16T03:58:11Z")

</div>

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

Strings are not strings.

---

<div class="post-metadata">

**Author:** ![RayLivingston](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@RayLivingston](https://forum.arduino.cc/u/RayLivingston)\
**Post date:** [July 16, 2023, 4:11am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/6 "2023-07-16T04:11:45Z")

</div>

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:

```arduino
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]

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [July 16, 2023, 4:19am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/7 "2023-07-16T04:19:53Z")

</div>

> [@RayLivingston](#):
>
> `<< Note this is unchanged, because the pp does not recognize id as a variable`

Yes, thanks. I should have seen that.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [July 16, 2023, 7:17am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/8 "2023-07-16T07:17:35Z")

</div>

> [@moorthypnt](#):
>
> ```arduino
> #define THINGNAME_1 id
> #define TOPICNAME_1 "$aws/things/" THINGNAME_1 "/shadow/update"
> 
> ```

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

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

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 16, 2023, 8:38am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/10 "2023-07-16T08:38:49Z")

</div>

don't use a String

this should work

```arduino
#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() {}

```

---

<div class="post-metadata">

**Author:** ![moorthypnt](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@moorthypnt](https://forum.arduino.cc/u/moorthypnt)\
**Post date:** [July 17, 2023, 4:20am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/11 "2023-07-17T04:20:29Z")

</div>

`#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.

---

<div class="post-metadata">

**Author:** ![moorthypnt](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@moorthypnt](https://forum.arduino.cc/u/moorthypnt)\
**Post date:** [July 17, 2023, 4:33am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/12 "2023-07-17T04:33:29Z")

</div>

it works 😍

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [July 18, 2023, 6:17am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/13 "2023-07-18T06:17:06Z")

</div>

But is wasteful on memory.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 14, 2024, 6:17am UTC](https://forum.arduino.cc/t/using-define-value-in-another-define-with-dynamically-loaded-first-define-value/1148521/14 "2024-01-14T06:17:53Z")

</div>

Closed on January 14, 2024, 6:17am UTC

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