2
I understand that it is possible to access variables dynamically in Javasscript, but to do this in C, I can’t find anywhere.
Follow the code in Javascript:
var n1,n2,n3,n4,n5;
n1 = n2 = n3 = n4 = n5 = 0; // define que todas essas variáveis são '0'
for (var i=1; i<=5; i++){
console.log( window["n"+i] ); // acessa a variável de nome 'n' + número do índice
}
// definimos novos valores para as mesmas variáveis
n1 = "Maçã";
n2 = "Pêra";
n3 = "Uva";
n4 = "Abóbora";
n5 = "Chuchu";
for (var i=1; i<=5; i++){
console.log( window["n"+i] ); // faz o mesmo que antes, mas agora as variáveis têm seus valores definidos
}
My question is, "How to do the same thing in C language?"
@OP as the bigown already said, only using "hashes". This is because it is really what Javascript does. Variable names in C are only abbreviations for places in memory
– Lucas Henrique
@Lucashenrique as in any language, in one form or another :)
– Maniero
really, what changes is the level of "iterations" =P
– Lucas Henrique