Thread: A problem with pointer initialization

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    A problem with pointer initialization

    Can anybody help me with this? I am writing a program for my assignment in college. We are working with lists. I have a problem with pointer initialization. The pointer is pointing at the first element of the list. I have a structure
    Code:
    typedef struct cvor
                        {
                            int broj;
                            struct cvor *sledeci;
                        }Tcvor;
    and a pointer type to the structure declared like this

    Code:
    typedef Tcvor  *Pcvor;
    This is the declaration of the pointer to the first element of the list.

    Code:
    Pcvor glava;
    I encounter a problem with printing and deleting a number from the list (and I can't know if the numbers are in the list at all). When I choose the option to run the selected function windows prints a message that the program has encountered an problem and needs to be shut down. After some debugging I realised that the pointer " *glava " was uninitialised even though the function for initialization did run.

    Code:
    void Inicijalizacija(Pcvor *glava)
    {
        *glava=NULL;
    }
    My friend wrote a program with same declarations and initialization function and it works. He worked in Ubuntu.

    The enviroment that I am working in is Code Blocks 8.02 for windows.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        typedef struct cvor
                            {
                                int broj;
                                struct cvor *sledeci;
                            }Tcvor;
        typedef Tcvor  *Pcvor;
    
    void Inicijalizacija(Pcvor *glava);
    void Osnovnimeni(char *izbor);
    void Unos(Pcvor *glava);
    void Brisanje(Pcvor *glava);
    void Listanje(Pcvor *glava);
    void Oslobodimemoriju(Pcvor *glava);
    
    main()
    {
        Pcvor glava;
        char izbor;
    
        Inicijalizacija(&glava);
        do
            {
                Osnovnimeni(&izbor);
                switch(izbor)
                {
                    case '1':   Unos(&glava);
                                break;
                    case '2':   Brisanje(&izbor);
                                break;
                    case '3':   Listanje(&izbor);
                                break;
                    case '4':   ;
                }
            }
        while(izbor!='4');
        Oslobodimemoriju(&glava);
    
    }
    
    void Inicijalizacija(Pcvor *glava)
    {
        *glava=NULL;
    }
    
    void Osnovnimeni(char *izbor)
    {
        char znak;
        printf("\n\n\t1) Dodaj\n\t2) Brisi\n\t3) Listaj\n\t4) Kraj?");
        do
        {
            printf("\n\t");
            *izbor = getchar();
    
        }while((*izbor > '4') || (*izbor < '1'));
    }
    
    void Unos(Pcvor *glava)
    {
        Pcvor tek,pret,novi;
        int br;
    
        novi = (Tcvor *)malloc(sizeof(Tcvor));
        printf("\n\n\tUnesite broj: ");
        scanf("%d", &br);
        novi->broj=br;
        novi->sledeci=NULL;
    
        if(*glava == NULL)
        {
            *glava = novi;
            return;
        }
        tek=*glava;
        pret=*glava;
        while(tek != NULL)
        {
            pret = tek;
            tek = tek->sledeci;
        }
        pret->sledeci=novi;
    }
    
    void Brisanje(Pcvor *glava)
    {
        Pcvor tek,pret;
        int br;
        printf("\n\n\tUnesite broj koji zelite da obrisete: ");
        scanf("%d", &br);
        if(*glava == NULL)
        {
            printf("\n\n\tLista je prazna!!!");
            return;
        }
    
        tek=*glava;
        pret=*glava;
        while(tek!=NULL && (tek->broj!=br))
        {
            pret=tek;
            tek=tek->sledeci;
        }
    
        if(tek == NULL)
            printf("\n\n\tZnaka nema u listi!!");
        else
        {
            if(tek==*glava)
            {
                *glava=tek->sledeci;
                tek->sledeci=NULL;
            }
            else
            {
                pret->sledeci=tek->sledeci;
                tek->sledeci=NULL;
            }
            free(tek);
        }
    }
    
    void Listanje(Pcvor *glava)
    {
        Pcvor tek;
        if(*glava == NULL)
        {
            printf("\n\n\tLista je prazna!!!");
            return;
        }
        printf("\n\n\tPrikaz liste:");
        tek = *glava;
        while(tek != NULL)
        {
            printf("%d\n",tek->broj);
            tek=tek->sledeci;
        }
    }
    
    void Oslobodimemoriju(Pcvor *glava)
    {
        Pcvor tek;
        while(*glava != NULL)
        {
            tek=*glava;
            *glava=tek->sledeci;
            free(tek);
        }
    }
    Here's the entire code. It maybe has some errors but I can'c correct them without correcting the pointer problem.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This is wrong:
    Code:
    void Inicijalizacija(Pcvor *glava)
    {
        *glava=NULL;
    }
    You are dereferencing the pointer with that asterisk. The function would be a silly one even if it worked (keep reading).
    This:
    Code:
    typedef Tcvor  *Pcvor;
    seems questionable to me. If you want a pointer to your new datatype Tcvor, just use this, and set to NULL:
    Code:
    Tcvor *ptr=NULL;
    So now the first 22 lines looks like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        typedef struct /* no need for a name */
                            {
                                int broj;
                                struct cvor *sledeci;
                            }Tcvor;
    
    void Osnovnimeni(char *izbor);
    void Unos(Pcvor *glava);
    void Brisanje(Pcvor *glava);
    void Listanje(Pcvor *glava);
    void Oslobodimemoriju(Pcvor *glava);
    
    main()
    {
        Tcvor *glava=NULL;
        char izbor;
    
        do
            {
    Notice I removed that silly function. Also, realize that char izbor is a single byte and cannot hold more than one character. Also note that glava is just a pointer and does not have any memory allocated to it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Corrected it and still reports the same error. :/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your updated code?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
        typedef struct cvor
                            {
                                int broj;
                                struct cvor *sledeci;
                            }Tcvor;
    //    typedef Tcvor  *Pcvor;
    
    void Inicijalizacija(Pcvor *glava);
    void Osnovnimeni(char *izbor);
    void Unos(Pcvor *glava);
    void Brisanje(Pcvor *glava);
    void Listanje(Pcvor *glava);
    void Oslobodimemoriju(Pcvor *glava);
    
    main()
    {
        Tcvor *glava=NULL;
        char izbor;
    
        Inicijalizacija(&glava);
        do
            {
                Osnovnimeni(&izbor);
                switch(izbor)
                {
                    case '1':   Unos(&glava);
                                break;
                    case '2':   Brisanje(&izbor);
                                break;
                    case '3':   Listanje(&izbor);
                                break;
                    case '4':   ;
                }
            }
        while(izbor!='4');
        Oslobodimemoriju(&glava);
    
    }
    
    /*void Inicijalizacija(Pcvor *glava)
    {
        *glava=NULL;
    }
    */
    
    void Osnovnimeni(char *izbor)
    {
        char znak;
        printf("\n\n\t1) Dodaj\n\t2) Brisi\n\t3) Listaj\n\t4) Kraj?");
        do
        {
            printf("\n\t");
            *izbor = getchar();
    
        }while((*izbor > '4') || (*izbor < '1'));
    }
    
    void Unos(Tcvor **glava)
    {
        Tcvor *tek,*pret,*novi;
        int br;
    
        novi = (Tcvor *)malloc(sizeof(Tcvor));
        printf("\n\n\tUnesite broj: ");
        scanf("%d", &br);
        novi->broj=br;
        novi->sledeci=NULL;
    
        if(*glava == NULL)
        {
            *glava = novi;
            return;
        }
        tek=*glava;
        pret=*glava;
        while(tek != NULL)
        {
            pret = tek;
            tek = tek->sledeci;
        }
        pret->sledeci=novi;
    }
    
    void Brisanje(Tcvor **glava)
    {
        Tcvor *tek, *pret;
        int br;
        printf("\n\n\tUnesite broj koji zelite da obrisete: ");
        scanf("%d", &br);
        if(*glava == NULL)
        {
            printf("\n\n\tLista je prazna!!!");
            return;
        }
    
        tek=*glava;
        pret=*glava;
        while(tek!=NULL && (tek->broj!=br))
        {
            pret=tek;
            tek=tek->sledeci;
        }
    
        if(tek == NULL)
            printf("\n\n\tZnaka nema u listi!!");
        else
        {
            if(tek==*glava)
            {
                *glava=tek->sledeci;
                tek->sledeci=NULL;
            }
            else
            {
                pret->sledeci=tek->sledeci;
                tek->sledeci=NULL;
            }
            free(tek);
        }
    }
    
    void Listanje(Tcvor **glava)
    {
        Tcvor *tek;
        if(*glava == NULL)
        {
            printf("\n\n\tLista je prazna!!!");
            return;
        }
        printf("\n\n\tPrikaz liste:");
        tek = *glava;
        while(tek != NULL)
        {
            printf("%d\n",tek->broj);
            tek=tek->sledeci;
        }
    }
    
    void Oslobodimemoriju(Tcvor **glava)
    {
        Tcvor *tek;
        while(*glava != NULL)
        {
            tek=*glava;
            *glava=tek->sledeci;
            free(tek);
        }
    }
    (if names of some functions are unclear)
    Inicijalizacija - Initialization
    Osnovnimeni - menu
    Unos - adding
    Brisanje - erasing
    Listanje - printing
    Oslobodimemoriju - freememory

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IF you would kindly post code that actually compiles next time.
    One of the problems is this. These functions takes a Tcvor** as argument, but you pass it a char*.
    Unos(&glava);
    Brisanje(&izbor);
    Listanje(&izbor);
    I still do not understand why you think that would work properly.
    Further, main returns int.

    Also, avoid typedefs of pointers.
    Your
    // typedef Tcvor *Pcvor;
    Only serves to complicate the code. Instead write it out using the real type and one or more *.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM