-1

I have created the following linked list that reads data from a file, and then dynamically allocates values to the linked list (I initialize it for example by providing A in fullname. I want the list to be available outside of the function but i think i have a problem there. By trying to print the list inside main i have nothing.. My code is below

#include <stdio.h>             
#include <string.h>             
#include <stdlib.h>             
int i,j,numberofseats,temp;     
char platenr[8],selection;      
char firstname[20],lastname[20]; 
char phone[11];                  
char *p;                         

typedef struct psg               
    {
    char fullname[40];
    unsigned short phonenr[10]; 
    unsigned int seatnr;        
    struct psg *next
    }PASSENGERS;                


void readfile(char *platenr, int *seatnr, PASSENGERS *passenger, PASSENGERS *tmp, PASSENGERS *start)
    {

    char buff[60];
    FILE *businfo;
    businfo = fopen ("bus.txt","r");
    if (businfo == NULL)
        {
        printf("Error Opening File, check if file bus.txt is present");
        exit(1);
        }
    else                                                                
        {
        fscanf(businfo,"%s %d",platenr, seatnr);      
        printf("Bus Licence plate Nr is: %s, and Number of Seats is: %d", platenr, *seatnr); 

        for (i=0;i<numberofseats;i++)
        {
        passenger  = (PASSENGERS *) malloc (sizeof(PASSENGERS));
        if (passenger==NULL)            /*elegxos orthis desmeysis mnimis*/
            {
            puts("Unable to allocate memory");
            exit(1);
            }
        passenger->next=NULL;
        strcpy (passenger->fullname,"A");
        passenger->seatnr=i+1;
        for (j=0;j<10;j++)
            passenger->phonenr[j]=0;
        if (start==NULL)
            start=passenger;
        else{
            tmp=start;
        while (tmp->next !=NULL) tmp=tmp->next;
        tmp->next=passenger;
            }
        }
        }

    }



int main()
{
PASSENGERS *passenger, *tmp, *start=NULL;
readfile(platenr,&numberofseats, passenger,tmp, start);

tmp=start;
while(tmp!=NULL)
    {
    printf ("%s",tmp->fullname);
    tmp=tmp->next;
    }

}
baskon1
  • 330
  • 1
  • 3
  • 15
  • Note that it is [redundant and potentially dangerous to cast the result of malloc and friends in C](http://stackoverflow.com/q/605845/253056). – Paul R Dec 19 '16 at 20:39
  • at first glance, make sure you new line your `printf` otherwise the buffer will not be flushed: `printf ("%s\n",tmp->fullname);` – Dellowar Dec 19 '16 at 20:40
  • Please post the output of what you have so far. – Dellowar Dec 19 '16 at 20:43
  • I added the new line in printf but didn't work out.. If i put the code for printing inside the function, it prints ok..Outside of the function it doesn't, so something is wrong with the result of the function being available in main – baskon1 Dec 19 '16 at 20:46
  • 1
    ` PASSENGERS *start) { PASSENGERS *tmp, *start=NULL; : `start` has duplicate names.` and Even if it does not overlap, thiis can not change the caller side variable. – BLUEPIXY Dec 19 '16 at 20:46
  • I had made a minor correction to the duplicates, that were left there after a minor revision of my code by mistake.. The problem is there though..I am sure, that i have a problem with pointers, but can't spot it out ! – baskon1 Dec 19 '16 at 21:02

1 Answers1

1

In side of Main you passing start as a pointer. You then change the value of start inside function readfile. The variable start in main and the variable start in readfile are not the same variable. For this code to work you would need to pass by reference.

As written the code will print nothing because the value of start inside main doesn't change.

some compilers will let you do the following...

readfile( ... , PASSENGERS *& start)

The above code would then make the variable start in main and the variable start in readfile the same variable.

if your compiler doesn't like that then you would need to pass a pointer to a pointer.

readfile( ... , PASSENGERS ** pStart)

Then inside your function declare and set start something like this

PASSENGERS * start = *pStart;

Cheers

Chadrick
  • 21
  • 5