How to Use Dynamic Name for Variables in C++

Is it possible to use dynamic name for variables in c++

It is not possible to do what you're asking, but there are alternatives that you should find equally expressive.

Probably the most common approach is to use a vector (or array) and index it:

std::vector<int> sol(2);
for (int i = 0; i < 2; ++i) {
sol[i] = i * i;
}

Another approach is to use a std::map to map the desired name to the resulting variable:

std::map<std::string, int> variables;
for (int i = 1; i < 3; ++i) {
std::string varname = "sol" + std::to_string(i);
variables[varname] = i * i;
}

Note, however, that this is an extremely slow solution. I mention it only because it allows you to do something similar to your original example. Use the vector / array approach instead.

Concatenating Variable Names in C?

When you find yourself adding an integer suffix to variable names, think I should have used an array.

struct mystruct {
int class[6];
};

int main(void) {
struct mystruct s;
int i;
for (i = 0; i < 6; ++i) {
s.class[i] = 1000 + i;
}

return 0;
}

Note: A C++ compiler will barf at this because of class. You will need to figure out a different name for that field if you plan to compile this code as C++.

Dynamic variable declaration in C

Unlike in interpreted languages, C does not have a dictionary of variable names at runtime. There exist no variable names at runtime at all. Hence unfortunately it is impossible to do what you want in C.

How do I name variables dynamically in C#?

Use a Dictionary<string, Variable>.

e.g.

var vartable = new Dictionary<string, Variable>();
vartable[strLine] = new Variable(input);

Create dynamic variable name

C# is strongly typed so you can't create variables dynamically. You could use an array but a better C# way would be to use a Dictionary as follows. More on C# dictionaries here.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickTest
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> names = new Dictionary<string,int>();

for (int i = 0; i < 10; i++)
{
names.Add(String.Format("name{0}", i.ToString()), i);
}

var xx1 = names["name1"];
var xx2 = names["name2"];
var xx3 = names["name3"];
}
}
}

Dynamic variables in C

What you need is something like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_COUNT 5

int main () {

int i;
int run = 0; //loop counter
char *str[MAX_COUNT];
char str_const[MAX_COUNT][20] = {
"abracadabra", "something", "more", "new", "old",
};
const char chr[MAX_COUNT] = {'a', 'e', 'r', 'e', 'd'};

for (i = 0; i < MAX_COUNT; i++) {
str[i] = malloc(strlen(str_const[i]) + 1);
strcpy(str[i], str_const[i]);
}

for (run = 0; run < MAX_COUNT; run++) { // runs 5 times
printf("The string %s will be removed from %c characters. \n", str[run], chr[run]);
rmchr(str[run], chr[run]);
printf("New modified string is: %s \n\n", str[run]);
}

return 0;

So here is the explanation:
You need a chr array that is basically an array of characters that you need. However, str needs to be an array of pointers to characters. So str array will just hold the address of the string and then you can use that address to access the string as you like. Hope that helps.

Dynamic Variable Name / Increment Variable Name

This is what is called a "variable variable."
A variable definition is comprised of two parts: the dollar sign, which tells the interpreter that it's a variable, and the variable name per se, which in short is a string; I'll call it "body" here.

So, if you have $var = 'my_other_var' and $my_other_var = 'hey', you can use the string "my_other_var" as the "body" of a the variable call.

Then echo $$var yields "hey".

Here with your example:

<?php

$var1 = 'A';
$var2 = 'B';
$var3 = 'C';
$var4 = 'D';

for ($i = 1; $i < 5; $i++) {

$varToEcho = "var$i"; // will become var1, var2, var3 and so on

echo $$varToEcho;

}


Related Topics



Leave a reply



Submit