Data Structures Debugging
Ok, the basic task for this program is to read input from a file that has a specific layout within the file. I read these into variables and do an sorted insertion into a singly linked list based on the value of time. The problem here is that I have lots of:
'invalid type argument of ->'
'deferencing pointer to incomplete type'
Most notably in the print_list function and sorted_insert function. Any pointers to why this is occuring would definitely be welcomed :)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#define MAX_TIMESTR 1024
/* Function Prototypes */
char *timestamp(time_t);
void sorted_insert(struct node** headRef, time_t, *char, int, double, int, int);
/* Debug Function Prototypes - Remove once Release */
void print_list(struct node* root)
{
struct node* current = root;
while (current != NULL){
printf("%s %s/%d %s\n",timestamp(current->data->time),
current->data->location, current->data->device_id,
(char)(current->data->msg));
current = current->next;
}
}
/* Data Structures */
struct message {
time_t time;
char location[24];
int device_id;
double frequency;
int burst_size;
int msg_len;
int *msg;
};
struct node {
struct node* next;
struct message data;
};
int main(int argc, char **argv)
{
int i = 1, a = 0;
time_t data_time;
char data_location[24];
int data_deviceid, burst_size, msg_len;
double data_frequency;
int *data_message;
struct node *empty = NULL;
/* Check for arguments if argc == 1 display usage */
if (argc == 1){
printf("usage: codebreak messagefile\n");
printf("where: messagefile contains intercepts in the correct binary \
format\n");
return 0;
}
/* Open a File for reading */
FILE *fp = fopen(argv[i],"r");
/* Place Data into Variables ready for insertion into SLL */
while ((a = fseek(fp,1,SEEK_CUR)) > 0){
fseek(fp,0,SEEK_CUR);
fscanf("%d %s %d %ld %d %d",&data_time,data_location,data_deviceid,
&data_frequency, &burst_size, &msg_len);
fseek(fp,1,SEEK_CUR);
data_message = malloc(msg_len*sizeof(int));
fgets(data_message,msg_len,fp);
sorted_insert(empty, data_time, data_location, data_deviceid,
data_frequency, burst_size, msg_len, data_message);
}
print_list(empty);
return 0;
}
void sorted_insert(struct node** headRef, time_t data_time, char data_location[],
int data_deviceid, double data_frequency, int burst_size, int msg_len,
int data_msg[])
{
struct node** currentRef = headRef;
struct node* new = (struct node*)malloc(sizeof(struct node));
/* Traverse until right node for insertion */
while(*currentRef != NULL && ((int)(data_time) < (int)((*currentRef)->data->time)))
currentRef = &((*currentRef)->next;
/* Insert data and re-link structures */
new->next = *currentRef;
*currentRef = new;
new->data->time = data_time;
for (int i = 0; i < 24; i++){
new->data->location[i] = data_location[i];
}
new->data->device_id = data_deviceid;
new->data->frequency = data_frequency;
new->data->burst_size = burst_size;
new->data->msg_len = msg_len;
new->data->msg = malloc(sizeof(msg_len * sizeof(int)));
for (int i = 0; i < msglen; i++){
new->data->msg[i] = data_msg[i];
}
}
char *timestamp(time_t tval)
{
static char t_buff[MAX_TIMESTR];
t_buff[MAX_TIMESTR-1] = '\0';
strftime(t_buff, MAX_TIMESTR-1, "%Y-%m-%d",gmtime(&tval));
return t_buff;
}