Thread: pointer problem or so...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    pointer problem or so...

    HI there, i am doing a TI scholing here in Holland, and for that i made a program that is part of a C and PLC programming project.

    I am working on my problem for about 3 weeks now and thought i would try to see if someone here can point me into the right direction to find my problem.

    in Case A of the Menu in Main i fill a linked list through a menu. it finds nicelyy the empty spot, puts the data in it what i dit put in, but the problem is that when i do that another time i have the data that i put in the second time seems to overwrite the date i put in the first time as well...

    Who can help me getting into the right direction...
    the complete code is below....

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    //#include "LaBOX_DIO.h"
    
    
    
    int parkeervak;
    char keuze;
    char identificatie[6];
    
    char *msg="******Welkom bij de garage van TI-groep 1 ****\n";
    char *msg0="******Welkom bij de garage van TI-groep 1 ****\n";
    char *msg1="De garage is op dit moment in gebruik";
    char *msg2="******een ogenblik geduld AUB.*******";
    char *msg3="***Sorry onze parkeerplaats is helaas vol***\n";
    char *msg4="U kunt wachten of het ergens anders proberen";
    char *msg5="**** keuze menu *****";
    char *msg6="A: Wilt U uw auto parkeren?";
    char *msg7="B: Wilt U uw auto ophalen?";
    char *msg8="S. Stoppen.";
    char *msg9="Maak een keuze:";
    char *msg10="Dit is foutieve keuze, kies opnieuw";
    char *msg11="***Een ogenblik geduld AUB uw auto wordt opgehaald***\n";
    char *msg12="***Een ogenblik geduld AUB uw auto wordt geparkeerd***\n";
    char *msg13="Uw auto staat klaar voor vertrek:\n";
    char *msg14="\n\n\n\n***** Voer uw ophaalcode in: *****\n";
    char *msg15="\n\n\n\n***** Uw auto is geparkeerd. *****\n";
    char *msg16="Wij proberen een parkeerplaats te reserveren\n";
    char *msg17="Het is helaas niet gelukt een parkeerplaats te reserveren\n***** probeert U het nog eens *****\n";
    char *msg18="***Error: kan de betreffende file niet openen**";
    char *msg90="***nog een prettige dag verder:**";
    char *msg99="***testmessage om werking te controleren:**";
    
    
    
    
    int controleerbusybit()                    /* controle van busybit 7 */
    {
    int p=0;
    	//LaBOX_Open();
    	//LaBOX_DigitalGetBit(7,&p);
    	//LaBOX_Close();
    	return p;
    }
    
    
    // aanmaken en parkeervak functies staan hieronder
    
    
    struct parkeervak {
        unsigned char Vak;
        char *Auto_Id;
        struct parkeervak* VolgendePtr;
        };
    
    struct parkeervak* NieuwParkeerVak(void)
    {
        void* ptr = malloc(sizeof(parkeervak)); // voeg nieuw item toe
     	return ((struct parkeervak*) ptr);  // return pointer naar nieuwe item
    };
    
    
    struct parkeervak* MaakParkeerVakken(int AantalVakken)
    {
        struct parkeervak* NieuwePtr;
        struct parkeervak* StartPtr;      // pointer naar 1ste item in de lijst
        int i;
        
        StartPtr = NieuwParkeerVak();     // alloceer item no 1
    
        NieuwePtr = StartPtr;
        for (i=0;i<AantalVakken-1;i++)    // nog de resterende parkeervakken   
        {   
            NieuwePtr->Vak = i;
            NieuwePtr->Auto_Id = "leeg";
                                    // alloceer volgend parkeervak
            NieuwePtr->VolgendePtr = NieuwParkeerVak(); 
            NieuwePtr = NieuwePtr->VolgendePtr;               
        };    
        NieuwePtr->Vak = i;
        NieuwePtr->Auto_Id = "leeg";
        NieuwePtr->VolgendePtr = NULL;     // einde van de lijst
        return (StartPtr);
    };
    
    
    
    void AlleParkeerVakken (struct parkeervak* StartPtr)
    {		                          // begin aan de kop van de lijst
         struct parkeervak* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL)  {
            printf("%d %s\n",HuidigePtr->Vak, HuidigePtr->Auto_Id);
            HuidigePtr = HuidigePtr->VolgendePtr;
         }
    };
    
    
    
    struct parkeervak* ZoekParkeerVak (struct parkeervak* StartPtr, int ZoekWaarde)
    {		                            // begin aan de kop van de lijst
         struct parkeervak* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Vak != ZoekWaarde)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
         if (HuidigePtr->Vak == ZoekWaarde)
            return (HuidigePtr);
         else 
            return (NULL);
    };
    
    
    
    struct parkeervak* ZoekAuto (struct parkeervak* StartPtr, char* ZoekWaarde)
    {		                        // begin aan de kop van de lijst
         struct parkeervak* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Auto_Id != ZoekWaarde)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
    	 printf("%s",HuidigePtr->Auto_Id);
         //if (HuidigePtr->Auto_Id == ZoekWaarde)
          if( !strcmp(HuidigePtr->Auto_Id, ZoekWaarde))
    		 return (HuidigePtr);
         else 
            return (NULL);
    };
    
    
    
    
    struct parkeervak* controleophaalcode (struct parkeervak* StartPtr, char* ZoekWaarde)
    {		                        // begin aan de kop van de lijst
         struct parkeervak* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Auto_Id != ZoekWaarde)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
         if (HuidigePtr->Auto_Id == ZoekWaarde)
            return (HuidigePtr);
         else 
            return (NULL);
    };
    
    
    
    struct parkeervak* controlevrijeplaats (struct parkeervak* StartPtr, char* ZoekWaarde)
    {		                        // begin aan de kop van de lijst
         struct parkeervak* HuidigePtr = StartPtr;
         while (HuidigePtr != NULL && HuidigePtr->Auto_Id != ZoekWaarde)  {
                HuidigePtr = HuidigePtr->VolgendePtr;
         }
         if (HuidigePtr->Auto_Id == ZoekWaarde)
            return (HuidigePtr);
         else 
            return (NULL);
    };
    
    //parkeer functies staan hieronder
    
    char reserveerparkeerplaats()
    {
    printf ("%s\n","voer uw kenteken in!");
    scanf("%s",&identificatie);
    return (identificatie);
    }
    
    
    void setparkeerbit()
    {
    }
    
    
    //ophaalfuncties staan hieronder
    
    char ophaalcodeinvoer()
    {
    	char c[6];
    printf ("%s\n","voer uw kenteken in!");
    scanf("%s",c);
    return c;
    }
    
    char setophaalbit()
    {
    }
    
    
    char geefparkeerplaatsvrij()
    {
    }
    
    
    
    
    
    
    
    
    int main()
    {
    unsigned int a=0;
    unsigned int b=0;
    int i=0;
    unsigned int x=0;
    char keuze;
    char c[6];
    
    
        struct parkeervak* StartPtr;      // pointer naar 1ste item in de lijst     
        struct parkeervak* Ptr;
        
        printf("programma start\n\n");
    
        StartPtr = MaakParkeerVakken(6);  // initialiseer de lijst met 6 items
            
        AlleParkeerVakken(StartPtr);      // Druk alles af
     
        system("PAUSE");                  // alleen voor eigen controle tijdens programmeren
    
    
    // menu
    	system("Cls");
    	do
    		{
    		if (controleerbusybit() == 1)		/* controle garage bezig zo ja blijven controleren tot garage vrij */
    			{
    			printf ("%s\n\n\n\n",msg3);
    			printf ("%s","******een ogenblik geduld AUB.*******");
    			Sleep (2000);
    			}
    		else										/* als garage niet bezig toon hoofdmenu */
    			{system("cls");
    				printf("%s\n\n\n\n",msg);
    				printf("%s\n\n",msg5);
    				printf("%s\n\n",msg6);
    				printf("%s\n\n",msg7);
    				printf("%s\n\n",msg8);
    				printf("%s\n\n",msg9);
    				msg=msg10;
    				scanf("%c",&keuze);
    				switch (keuze)
    				{
    				case 'A':              /* Parkeergedeelte van het menu */
    										{
    						Ptr = controlevrijeplaats(StartPtr,"leeg");		// controle of er een vrije plaats is
    						if (Ptr != NULL)                  // gevonden?
    						{
    							printf("Vak %d %s gevonden\n",Ptr->Vak,Ptr->Auto_Id);
    							printf("Pointer %d is nu huidig\n",Ptr);
    							Sleep (2000);
    							reserveerparkeerplaats();					// reserveren van de gevonden parkeerplaats
    							Ptr->Auto_Id = identificatie;
    							printf("Pointer %d is nu huidig\n",Ptr);
    							AlleParkeerVakken(StartPtr);      // Druk alles af ter controle alleen tijdens programmeren
    							printf("Pointer %d is nu huidig",Ptr);
    								system("Pause");
    
    							printf("Leeg vak %s gevonden\n %d",Ptr->Auto_Id, Ptr->Vak); // alleen voor eigen controle tijdens programmeren
    							printf("Leeg vak %d gevonden\n",Ptr->Vak);					// alleen voor eigen controle tijdens programmeren
    							Sleep (2000);
    							system("Pause");											// alleen voor eigen controle tijdens programmeren
    							AlleParkeerVakken(StartPtr);								// alleen voor eigen controle tijdens programmeren
    							break;
    							setparkeerbit();							// zetten van de parkeerbit voor de gevonden plaats
    							do
    								{
    									if (controleerbusybit()== 0)
    									{
    										system("cls");					// melding bezig met parkeren
    										Sleep(500);
    										printf("%s\n\n\n\n\n\n\n",msg12);
    										Sleep(3000);
    																			}
    									else
    									{
    										system("cls");					//melding auto geparkeerd
    										printf("%s\n\n\n\n\n\n\n",msg15);
    										printf("%s\n\n\n\n\n\n\n",msg90);
    										Sleep(3000);
    									}
    								}
    								while (controleerbusybit()!= 1);         // controle of de garage nog bezig is met parkeren
    							break;
    						}
    
    						else
    						{
    							system("cls");
    							printf("%s\n\n\n\n\n\n\n",msg3);
    							break;
    						}
    					}
    
    				case 'B':              /* Ophaalgedeelte van het menu */
    					ophaalcodeinvoer(identificatie);						// 
    					Ptr = ZoekAuto(StartPtr,"vol");		// controle of de auto in de parkeerplaats staat
    						if (Ptr != NULL)                  // gevonden?
    						{
    							printf("auto met code %s in vak %d gevonden\n",Ptr->Auto_Id, Ptr->Vak);
    							Sleep (2000);
    							geefparkeerplaatsvrij();
    							Ptr->Auto_Id = geefparkeerplaatsvrij;
    							system("cls");
    							printf("%s\n\n\n\n\n\n\n",msg3);
    							setophaalbit();
    							do
    								{
    									if (controleerbusybit()== 0)
    									{
    										system("cls");					// melding bezig met parkeren
    										Sleep(500);
    										printf("%s\n\n\n\n\n\n\n",msg12);
    										Sleep(3000);
    																			}
    									else
    									{
    										system("cls");					//melding auto geparkeerd
    										printf("%s\n\n\n\n\n\n\n",msg15);
    										printf("%s\n\n\n\n\n\n\n",msg90);
    										Sleep(3000);
    									}
    								}
    								while (controleerbusybit()!= 1);         // controle of de garage nog bezig is met parkeren
    							break;
    
    						}
    
    						else
    						{
    							printf("Niets gevonden\n");
    							Sleep(3000);
    							break;
    						
    						}
    
    					
    		
    				case 'S':              /* Einde van het programma */
    					
    						return;
    					}
    
    
    
    
    
    
    
    
    
    
    
    				}
    		}
    		
    
    	while(1);
    }

    Regards Fred

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    TL62, can you fix your post so it's readable?

    It's so W-I-D-E I can't read it. Three space code indentations is preferable to tab's or 10 spaces!

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I didn't read much of your code but your problem seems to be this
    Code:
    Ptr->Auto_Id = identificatie;
    identificatie is a global char array, this way you set the Auto_Id member of your stuct pointing to the same buffer for each record. -> if you input a new string value into identificatie it changes for each record.
    You have to copy the data.
    e.g. like this
    Code:
    Ptr->Auto_Id = malloc(strlen(identificatie)+1);
    strcpy(Ptr->Auto_Id, identificatie);
    And don't forget to free the pointer when done with a record.

    Another thing I noticed ( without looking much )
    Code:
    char reserveerparkeerplaats()
    {
    printf ("%s\n","voer uw kenteken in!");
    scanf("%s",&identificatie);
    return (identificatie);
    }
    I'd change this to
    Code:
    char * reserveerparkeerplaats()
    {
        printf ("%s\n","voer uw kenteken in!");
        scanf("%s",identificatie);  /* identificatie is a pointer */
        return identificatie;
    }
    Kurt

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    It's so W-I-D-E I can't read it. Three space code indentations is preferable to tab's or 10 spaces!
    Nah, tabs are fine, but indentation might need a little work and all the comments are written in non-english (german is my best guess), so it's a little hard to understand the code.
    If it's too wide, you can try to copy it into the IDE or something.
    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.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    char *msg="******Welkom bij de garage van TI-groep 1 ****\n";
    I never expected this to be valid C code, but it does compile. It looks dodgy though as if you aren't allocating memory for the pointer how can you sure you're not going to be overwriting something else?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To be better, it should be const char*, but otherwise it's perfectly valid and works fine.
    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.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Oh yeah, I guess if you initailize it as you declare it the compiler know how much space is needed. And yes, const char* would be more suitable

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by mike_g View Post
    I never expected this to be valid C code, but it does compile. It looks dodgy though as if you aren't allocating memory for the pointer how can you sure you're not going to be overwriting something else?
    It's fine.

    char *msg makes a pointer to a char, and = "string" initializes it to point to read-only memory containing "string".

    edit: too slow.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Quote Originally Posted by Adak View Post
    TL62, can you fix your post so it's readable?

    It's so W-I-D-E I can't read it. Three space code indentations is preferable to tab's or 10 spaces!

    Will try to do so tonight


    About te remark of the Dutch comments, yeah i know it's hard to understand when it's nor commented in English, am working on that also but also working on the program itself as i need to get it running within a week, So trying to get it translated asap. It's just annoying that i am making some stupid errors somehow,

    Just found out that if i get back from a struct into the main where i check it for NULL it just crashes on me.
    I am really looking at the screen not knowing what to do...

    am trying to work around with the things Kure gave me..... any other help would be welcome...

    TIA
    Last edited by TL62; 01-12-2008 at 12:41 PM.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Quote Originally Posted by Elysia View Post
    To be better, it should be const char*, but otherwise it's perfectly valid and works fine.

    will take it in considderation with my next project:-) as long as this works without hasseling me too much i am happy:-)

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can remember that when doing char* str = "something" it's read-only - you can't change it or you'll get a crash. Therefore, it's better to use const char* str = "something" since the compiler won't allow you to change it then.
    If you're wondering why.
    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.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Nah, tabs are fine,
    Until you start mixing spaces and tabs, or in your IDE change the default width to something other than 8.
    Then you're back to the same old mess as before.
    If you only use spaces, then the code will look the same no matter what editor you use, and no matter what forum you post it on.
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you only use tabs the indentation will look right wherever you post it and wherever you use it, whatever the editor.
    Default I use 4 spaces for tabs, though.
    There are arguments for spaces and for tabs, as well, and last time, even though spaces slightly won, the issue seems that many prefer tabs and many prefer spaces, and there's no clear winner.
    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.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Yeah, but if you change the indent to 4, and then post it somewhere else which uses 8, then it may be indented OK, but it won't look like the code in your IDE.
    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.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But then I would have to tab back 4 times instead of one to delete (or press space 4 times to ident) those spaces when you post the code somewhere.
    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. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM