error: request for member 'fix' in something not a structure or union

0

The program tries to implement an array with a buffer but sends an error with a message that says:

  

error: request for member 'fix' in something not to structure or union

How can this error be resolved?

#include <stdio.h>
#include <stdbool.h>
#include <conio.h>
#define N 30
typedef struct {  int arre[N];
int tope; 
} ArregloConTope;
/* función que me dice si el arreglo ya se llenó */
bool EstaLleno (ArregloConTope act)
{
    bool lleno = false; 
    if (act.tope == N)
        lleno = true;
    return lleno; 
}
/* procedimiento que inserta un valor en el arreglo con tope */
/* precondición: el arreglo no debe estar lleno */
void Insertar (ArregloConTope *act, int valor) 
{
    /* inserto el valor contra el tope y luego lo incremento, como forma de indicar que ahora tengo un valor más */
    int posicion = act->tope;
    act.arreglo[posicion] = valor;
    act->tope++;
}
int main(){
    ArregloConTope *act; int valor;
    int op;
    printf("\t\tBienvenido al programa de arreglo con tope\n");
    printf("1.Insertar elemento\n");
    printf("2.Verificar si el vector esta lleno\n");
    printf("Ingrese una opcion ");
    scanf("%d",&op);
    switch(op){
    case 1:
        Insertar(act, valor);
        break;
    case 2:
        printf("%d", EstaLleno(*act));
    default:
        printf("Opcion no valida");   
    }
    getch();
    return 0;
}
    
asked by Alejandro Caro 26.11.2017 в 00:32
source

2 answers

1

The structure defines a member named arre :

typedef struct {
  int arre[N];  // <<--- AQUI!!!
  int tope; 
} ArregloConTope;

And yet then you try to access member arreglo :

void Insertar (ArregloConTope *act, int valor) 
{
    int posicion = act->tope;
    act.arreglo[posicion] = valor; // <<--- AQUI!!!
    act->tope++;
}

The solution is as simple as replacing arre with arreglo or vice versa.

On the other hand, notice that in the main you are declaring an uninitialized pointer:

int main(){
    ArregloConTope *act; // <<--- Declaras puntero sin inicializar
    int valor;
    int op;
    printf("\t\tBienvenido al programa de arreglo con tope\n");
    printf("1.Insertar elemento\n");
    printf("2.Verificar si el vector esta lleno\n");
    printf("Ingrese una opcion ");
    scanf("%d",&op);
    switch(op){
    case 1:
        Insertar(act, valor); // <<--- Y aqui lo usas

With that the application will access (through the pointer) memory that does not belong to it and the Operating System, to prevent corrupting memory, will kill the process. The result is a nice segmentation error and the application to the gare.

For this particular case it is not necessary that you use dynamic memory, so for now I would save it (at the moment there is enough to understand the rest of the language). Even so, do not forget to initialize the variables correctly to avoid strange behavior:

int main()
{
  ArregloConTope act;
  act.tope = 0; // <<--- Importante

  // ...

  Insertar(&act,valor);

  // ...

  printf("%d", EstaLleno(act));
}

By the way, note that at no time are you asking the user to fill in the variable valor ... then do not complain if you fill the array with garbage.

    
answered by 27.11.2017 / 09:44
source
0

Here is the full program working.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <conio.h>
/* defino un tipo que representa un arreglo con tope de enteros */
#define N 30
typedef struct {
int arre[N];
int tope;
} ArregloConTope;
/* función que me dice si el arreglo ya se llenó */
bool EstaLleno (ArregloConTope act)
{
    bool lleno = false; 
    if (act.tope == N)
        lleno = true;
    return lleno; 
}
/* procedimiento que inserta un valor en el arreglo con tope */
/* precondición: el arreglo no debe estar lleno */
void Insertar (ArregloConTope *act, int valor) 
{
    /* inserto el valor contra el tope y luego lo incremento, como forma de indicar que ahora tengo un valor más */
    int posicion = act->tope;
    act->arre[posicion] = valor;
    act->tope++;
}
/* procedimiento que muestra el arreglo completo*/
/* precondición: el arreglo debe tener al menos un elemento */
void MostrarArreglo (ArregloConTope act){
int x;
for(x=0;x<act.tope;x++)
{
printf("%d ", act.arre[x]);
}
getchar();
}

int main()
{
ArregloConTope act;
int valor;
int op;
act.tope = 0;
while(true){
system("cls");
printf("\t\tBienvenido al programa de arreglo con tope\n");
printf("1.Insertar elemento\n");
printf("2.Mostrar arreglo\n");
printf("3.Verificar si el vector esta lleno\n");
printf("4.Salir\n");
printf("Ingrese una opcion ");
scanf("%d",&op);
switch(op)
{
case 1:
if (act.tope != N){
printf("Ingrese valor ");
scanf("%d", &valor);
Insertar(&act, valor);
}else{
printf("Se lleno el arreglo");
getch();
}
break;
case 2:
MostrarArreglo(act);
getchar();
break;
case 3:
if (EstaLleno(act))
printf("El arreglo esta lleno");
else
printf("Aun queda espacio");
getch();
break;
case 4:
getchar();
return 0;
default:
printf("Opcion no valida");
}
}
getch();
return 0;
}
    
answered by 04.12.2017 в 02:24