Name: Anonymous 2010-03-26 20:36
Hi /prog/,
so I'm new into ANSI C because you guys always advocate for it. I crammed together this piece of code but it seems that I did not really understand why I had to use a double-reference in the function call. Could you please enlighten me!
so I'm new into ANSI C because you guys always advocate for it. I crammed together this piece of code but it seems that I did not really understand why I had to use a double-reference in the function call. Could you please enlighten me!
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct stack{
int value;
struct stack *next;
} STACK;
STACK s;
void push(STACK **, int);
int pop(STACK **, int *);
int main(void){
STACK *p, **pp;
p = &s; pp = &p;
*pp = NULL;
int i, x, error;
for(i = 0; i < MAX; i++){
push(pp, i);}
for(i = 0; i < MAX+1; i++){
x = pop(pp, &error);
if(!error) printf("pop (): %d\n", x);
else printf("error: stack empty\n");}
return 0;
}
void push(STACK **s, int item){
STACK *new;
new = (STACK *) malloc(sizeof(STACK));
new->value = item;
new->next = *s;
*s = new;}
int pop(STACK **s, int *error){
STACK *old = *s;
int oldValue;
if(*s){
oldValue = old->value;
*s = (*s)->next;
free(old);
*error = 0;}
else{
oldValue = 0;
*error = 1;}
return(oldValue);}