Can I declare a variable with an uncertainty number suffix?

Joined
Mar 5, 2024
Messages
2
Reaction score
0
Hi all,

I want to declare a variable whose name is decided by a variable value.
such as: if idx=0, then I need a new variable named var_00; if idx=1, then I need a new variable named var_01; ...; if idx=255, then I need a new variable named var_FF;
I don't know if it can be realized by '#' and '##', can anyone help me?
Thanks a lot!

C:
#include <stdio.h>
#include <stdint.h>

#define NAME(a)     p_##a

int main()
{
    uint8_t idx = 0;
    
    uint8_t* NAME(idx) = NULL;
    
    p_idx = &idx;       // not p_idx, I want p_00;
    
    printf("*p_idx = %d\n", *p_idx);
    
    return 0;
}
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
Hi all,

I want to declare a variable whose name is decided by a variable value.
such as: if idx=0, then I need a new variable named var_00; if idx=1, then I need a new variable named var_01; ...; if idx=255, then I need a new variable named var_FF;
I don't know if it can be realized by '#' and '##', can anyone help me?
Thanks a lot!

C:
#include <stdio.h>
#include <stdint.h>

#define NAME(a)     p_##a

int main()
{
    uint8_t idx = 0;
   
    uint8_t* NAME(idx) = NULL;
   
    p_idx = &idx;       // not p_idx, I want p_00;
   
    printf("*p_idx = %d\n", *p_idx);
   
    return 0;
}
Dictionaries (or Maps) are data structures that store key-value pairs. You can use the value of idx as the key and create the desired variable value as the corresponding value in the dictionary.
 
Joined
Nov 23, 2023
Messages
59
Reaction score
3
Yes, you can declare a variable with an uncertainty number suffix, especially in scientific or technical contexts where uncertainty is a crucial aspect of measurement and analysis.
 
Joined
Nov 24, 2022
Messages
92
Reaction score
7
Technically I suppose all C compilers have variable list (each type, address, and place for value) You need to create such list. In javascript would be easier. I think it's very ,very restricted place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,440
Members
47,101
Latest member
DoloresHol

Latest Threads

Top