So I'm doing this stack program , and basically when I try to print the elements in the stack, nothing happens... I'm totally lost : ( , any help would be appreciated.

Oh and some function names are in spanish, llena means full, vacia means empty, the variable cuenta is basically the top of the stack. and imprime means print :)

#include <stdio.h>
#include <stdlib.h>
#define N 10

typedef int elem;

 struct pila{
   elem datos[N];
   int cuenta;
   }p;
   
typedef struct pila tpila;        
        
  tpila inicializa(tpila p){
        p.cuenta=-1;
      return p;
      }
      
   int vacia(tpila p){
       if(p.cuenta==-1)
         return 1;
        return 0;
        }
        
    int llena(tpila p){
        if(p.cuenta==N-1)
          return 1;
         return 0;
         }
         
    elem tope(tpila p){
         if(!vacia(p))
          return p.datos[p.cuenta];
          }       
          
    tpila push(tpila p,elem x){
          if(!llena(p)){
            p.datos[p.cuenta+1]=x;
            ++p.cuenta;
            }
           return p;
             }
           
           
     tpila pop(tpila p){
           if(!vacia(p))
             p.cuenta--;
           return p;
           }      
           
     void imprime(tpila p){
          int i;
          for(i=p.cuenta;i>=0;i--)
            printf("\n%i\n",p.datos[i]);
            }
           
         
int main()
{
    int op,x;
    inicializa(p);
  do {
      
    printf("Selecciona una opcion:\n1-Push\n2-Pop\n3-Tope\n4-Imprime\n5-Copiar a Pila 2\n6-Imprime Pila 2\n7-Salir\n");
     scanf("%i",&op);
     switch(op){
      case 1:
            printf("Escribe el numero a insertar\n");
          scanf("%i",&x);
            push(p,x);
        break;
      case 2:pop(p); 
        break;
      case 3: tope(p);
        break;
      case 4: imprime(p);
        break;
                 }
             }while(op!=7);  	
  return 0;
}

By elements on the stack, do you mean arguments passed to the program at start up or do you mean displaying everything that's on the stack for the user program?

By elements on the stack, do you mean arguments passed to the program at start up or do you mean displaying everything that's on the stack for the user program?

just displaying the numbers on the stack

if you want the values on the stack then try something like this

Note this works on a 64 bit machine

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

int main(int argc, char**argv)
{
  long x = 1234;//stack
  long y = 5678;//stack
  void *vptr = &y;//address of the bottom of the stack

  fprintf(stdout, "value->%p, %d\n", vptr, *(long*)vptr);
  vptr += sizeof(void*);
  fprintf(stdout, "value->%p, %d\n", vptr, *(long*)vptr);
  vptr += sizeof(void*);

  fprintf(stdout, "ans->%d %p, %d %p\n", x, &x, y, &y);

  exit(EXIT_SUCCESS);
}

Edited 7 Years Ago by gerard4143: n/a

you forgot to use the return value of your structure just place p=func(p);
or if u want to use p to initialize and then use it's value u can use ptr to a structure

also why do u intilise first time just use the push function
when u want to intilise and when u want to delete u should pop it

if you want the values on the stack then try something like this

Note this works on a 64 bit machine

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

int main(int argc, char**argv)
{
  long x = 1234;//stack
  long y = 5678;//stack
  void *vptr = &y;//address of the bottom of the stack

  fprintf(stdout, "value->%p, %d\n", vptr, *(long*)vptr);
  vptr += sizeof(void*);
  fprintf(stdout, "value->%p, %d\n", vptr, *(long*)vptr);
  vptr += sizeof(void*);

  fprintf(stdout, "ans->%d %p, %d %p\n", x, &x, y, &y);

  exit(EXIT_SUCCESS);
}

errr, I just would like a cycle to print the stack..

This article has been dead for over six months. Start a new discussion instead.