Pointer to struct in ansi C

3

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

    
asked by Guido Dicomo 20.10.2017 в 17:14
source

1 answer

4

Your problem basically is that you pass only the pointer, if the pointer is NULL, the assignment is completed, but it is never returned to ProximoToken .

I recommend changing the signature of the Buscar function to the following:

int Buscar(char *id, T_REG *TS, T_REG **reg);

You do the assignment within the function in the following way:

*reg = &TS[i];

And when calling the function, remember to pass the pointer address you need:

Buscar(id, TS, &reg);

Fixed your code would look like this:

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;
}

This way you pass the pointer address in reg and make the address change when necessary if the token is found.

Edit: In response to your new problem

In this line:

T_REG **reg;

You put a pointer to pointer, but you never put any memory to use that pointer, you can use two ways:

T_REG *reg;

And perform a normal pointer assignment and then return it:

reg = &TS[i];

Or you can place dynamic memory (In heap ) to use the pointer:

T_REG **reg = malloc(sizeof(T_REG *));

And use the same assignment:

*reg = &TS[i];

Your "crashing" program because the pointer to pointer reg is not pointing to a valid memory address ( NULL ) and you need to put memory to use it, or change it to a simple pointer.

    
answered by 20.10.2017 / 17:53
source