C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-17-2009, 09:41 AM   #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.
zyklon is offline   Reply With Quote
Old 01-17-2009, 10:04 AM   #2
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,183
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 01-17-2009, 10:16 AM   #3
Registered User
 
Join Date: Jan 2009
Posts: 3
Corrected it and still reports the same error. :/
zyklon is offline   Reply With Quote
Old 01-17-2009, 10:40 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,338
What is your updated code?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 01-17-2009, 10:50 AM   #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
zyklon is offline   Reply With Quote
Old 01-17-2009, 12:42 PM   #6
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,783
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 *.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22