So I'm doing an exercise in C, learning to manipulate data structures. I am struggling a little with structs/pointers in functions. I've tried to step through, and I can see that I'm creating new structures, but I'm either creating them in the same memory location OR I'm creating a Linked List with multiple of the same struct! I can't work out what is going on.
I've attempted to simplify my code as requested, and this now contains all of the code (I believe the minimum?) required to reproduce my problem.
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct aircraft {
char FlightNumber[9]; // Unique aircraft ID
struct aircraft * next; // A pointer to the next aircraft in the current queue
} AIRCRAFT;
AIRCRAFT *AirQueue = NULL;
AIRCRAFT *Current = NULL;
void GenerateFlightNumber(AIRCRAFT* node) {
char FlightNumber[10] = ""; // Stores the flight number for the duration of this function
int random = 0; // Stores the random number used to generate the flight number prefix and suffix
// Generates the prefix
srand((unsigned)time(NULL)); // Uses current time as seed for random generator
random = rand() % 10; // Generates a random number between 0 and 9
char prefix[10][5] = { "BA","ELAL","SHT","EXS","EZY","TOM","RYR","MON","UAE","TFL" }; // Array of airline prefixes
strcpy_s(FlightNumber, sizeof(FlightNumber), prefix[random]); // Copies a prefix to the FlightNumber variable by selecting one using a random index number
// Generates the suffix
char suffix[5]; // Stores the flight number suffix
srand((unsigned)time(NULL)); // Uses current time as seed for random generator
random = (rand() % 8888) + 1111; // Generate a random number between 1111 and 9999
_itoa_s(random, suffix, 5, 10); // Converts the randomly generated suffix to a string, and stores it in the suffix variable
strcat_s(FlightNumber, sizeof(FlightNumber), suffix); // Concatenates the prefix and suffix to the FlightNumber variable
strcpy_s(node->FlightNumber, sizeof(node->FlightNumber), FlightNumber); // Assign the final flight number to the new aircraft
}
void setUpAircraft(AIRCRAFT * node, bool ground) {
GenerateFlightNumber(node);
}
AIRCRAFT* StartAirQueue()
{
printf("\nCreating Air Queue...");
AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
if (Temporary == NULL)
{
printf("\nFailed to allocate memory\n");
return NULL;
}
setUpAircraft(Temporary, false);
Temporary->next = NULL;
AirQueue = Current = Temporary;
return Temporary;
}
AIRCRAFT* AddToAirQueue(bool end)
{
if (NULL == AirQueue)
{
return (StartAirQueue());
}
if (end)
printf("\nAdding node to end of queue...");
else
printf("\n Adding node to beginning of queue...");
AIRCRAFT *Temporary = (AIRCRAFT*)malloc(sizeof(AIRCRAFT));
if (NULL == Temporary)
{
printf("\n Node creation failed \n");
return NULL;
}
setUpAircraft(Temporary, false);
Temporary->next = NULL;
if (end)
{
Current->next = Temporary;
Current = Temporary;
}
else
{
Temporary->next = AirQueue;
AirQueue = Temporary;
}
return Temporary;
}
void print_list(void)
{
AIRCRAFT *ptr = AirQueue;
printf("\n -------Printing list Start------- \n");
while (ptr != NULL)
{
printf("\n [%s] \n", ptr->FlightNumber);
ptr = ptr->next;
}
printf("\n -------Printing list End------- \n");
return;
}
int main(void)
{
int i = 0, result = 0;
AIRCRAFT *ptr = NULL;
print_list();
for (i = 5; i < 10; i++)
AddToAirQueue(true);
print_list();
for (i = 4; i > 0; i--)
AddToAirQueue(false);
print_list();
getchar();
return 0;
}
I want to produce a list of aircraft, each with a different flight number. As you can see from the result, each node in the list seems to contain the same flight number.
------Printing list Start-------
------Printing list End------
Creating Air Queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
------Printing list Start-------
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
------Printing list End------
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
Adding node to end of queue...
------Printing list Start-------
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
[RYR5769]
------Printing list End------
Does anyone have any helpful suggestions as to why my list seems to contain the same node repeatedly?