I have defined the following globally.
char buffer[32+1];
TOKEN tokenActual;
int flagToken = 0;
typedef enum{
INICIO, FIN, LEER, ESCRIBIR, ID, CONSTANTE, PARENIZQUIERDO, PARENDERECHO,
PUNTOYCOMA, COMA, ASIGNACION, SUMA, RESTA, FDT, ERRORLEXICO
}TOKEN;
typedef struct{
TOKEN clase;
char nombre[32+1];
int valor;
}T_REG;
T_REG TS[1000] = {{INICIO, "inicio", 99},
{FIN, "fin", 99},
{LEER, "leer", 99},
{ESCRIBIR, "escribir", 99},
{99, "$", 99}};
and inside the main I have this function:
TOKEN ProximoToken(){
T_REG *reg;
tokenActual = scanner(); /*scanner analiza un flujo, devuelve un TOKEN y en buffer almacena lo analizado*/
if(tokenActual == ID) Buscar(buffer, TS, reg);
printf("*** Encontre nombre[%s] clase[%d]\n", reg->nombre, reg->clase);
return tokenActual;
}
and Search:
int Buscar(char *id, T_REG *TS, T_REG *reg){
int i = 0;
while(strcmp("$", TS[i].nombre)){
if(!strcmp(id, TS[i].nombre)){
reg = &TS[i];
printf("*** Buscar: nombre[%s] clase[%d]\n", reg->nombre, reg->clase);
return 1;
}
i++;
}
return 0;
}
My problem is that when I call "Search" I pass buffer="home", I look for it in TS and when it finds it, it prints on the screen:
*** Buscar: nombre[inicio] clase[0]
The issue is that when it finishes Search and I return to ProximoToken, I understand that "reg" points to a position in TS. And when I try to print on the screen the values of that targeted structure, it prints garbage.
What am I doing wrong?
I clarify that this is part of a "pseudo compiler" that analyzes a file with code and evaluates it. For example:
inicio
leer (a,b);
cc := a + (b-2);
escribir (cc, a+4);
fin
I have a function that returns a point to pointer:
T_REG** Colocar(char *id, T_REG *TS){
T_REG **reg;
int i = 4;
while(strcmp("$", TS[i].nombre)) i++;
if(i < 999){
strcpy(TS[i].nombre, id);
TS[i].clase = ID;
TS[i].valor = 0;
*reg = &TS[i];
strcpy(TS[++i].nombre, "$");
return reg;
}
}
This function is intended to add an identifier to the TS list and return a pointer to that aggregate element.
in% co_of% bust. why? Do pointers to pointers have to be initialized? if I ask *reg = &TS[i];
tmb bust