Thread: Questions About Stack

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    2

    Questions About Stack

    Code:
    #include<stdio.h>
    #include<malloc.h>
    struct snode{
        int data;
        struct snode *next;} ;
    void begin(struct snode *head);
    void push(struct snode *head,int t);
    void pop(struct snode *head,int *d);
    void main(void){
        struct snode *first;
        begin(first);
        int i,x;
        for(i=1;i<10;i=i+2){
            push(first,i);}
        while(first->next!=NULL){
            pop(first,&x);
            printf("%d",x);}}
    void begin(struct snode *head){
        head=malloc(sizeof(struct snode));
        head->next=NULL;
    }
    void push(struct snode *head,int t){
        struct snode *p;
        p=malloc(sizeof(struct snode));
        p->data=t;
        p->next=head->next;
        head->next=p;}
    void pop(struct snode *head,int *d){
        struct snode *p;
        p=head->next;
        head->next=p->next;
        *d=p->data;
        free(p);
    }

    When I compile the program above, it warned that in the line "begin(first)", "first"is used uninitialized in the function. But I have already declared as "struct snode *first".
    What's my problem?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Yes it's declared, but the error message is about initialisation, not declaration.

    Code:
    #include<stdio.h>
    #include<stdlib.h> /*!! malloc.h is obsolete, use stdlib for malloc */
    
    struct snode {
        int data;
        struct snode *next;
    };
    
    void begin(struct snode *head);
    void push(struct snode *head, int t);
    void pop(struct snode *head, int *d);
    
    int main(void)  /*!! main returns int, not void */
    {
        struct snode *first = NULL; /*!! make sure it's initialised */
        begin(first);
        int i, x;
        for (i = 1; i < 10; i = i + 2) {
            push(first, i);
        }
        while (first->next != NULL) {
            pop(first, &x);
            printf("%d", x);
        }
    
        return 0;
    }
    
    void begin(struct snode *head)
    {
        head = malloc(sizeof(struct snode));
        head->next = NULL;
    }
    
    void push(struct snode *head, int t)
    {
        struct snode *p;
        p = malloc(sizeof(struct snode));
        p->data = t;
        p->next = head->next;
        head->next = p;
    }
    
    void pop(struct snode *head, int *d)
    {
        struct snode *p;
        p = head->next;
        head->next = p->next;
        *d = p->data;
        free(p);
    }
    Be consistent about your indentation.
    Indent style - Wikipedia, the free encyclopedia
    Note that multiple closing braces on the same line are particularly hard to follow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about stack
    By student111 in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2013, 07:01 AM
  2. Stack and Queues questions
    By ee1215 in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2011, 07:19 PM
  3. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  4. stack
    By qqqqxxxx in forum C Programming
    Replies: 11
    Last Post: 02-08-2006, 06:09 PM
  5. <<simple stack program questions
    By rdt253 in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2005, 03:10 AM

Tags for this Thread