0

I am having issues. I am trying to create a link list and I get these errors.I don't know if I am passing the pointers correctly. I cant even get my program to run right.

I am trying to take data from a a file and put it in to a link list with out a global head (i have to pass it) ( this is considered head1) Then, using the link list I made, I need to make 16 link lists (create then destroy, this is head2) of a size of six letters each. ex. take in the first six, then the next six etc. I have it somewhat constructed, but I don't know where to start.

Error Message:

invalid application of 'sizeof' to incomplete type 'struct node' line 85

request for member 'nextData' in something not a structure or union line 91 ( just an example, there are many of these areas.)

Code:

  struct boggleDataNode {
    //(line 14) This is the data portion of the node? So we are storing 96 arrays? or 96 nodes?
    char data[3];
    // (line 16) Is this the pointer variable? This points to the next node in the link list correct?
    struct boggleDataNode *nextData;
};
struct boggleDieSideNode {

    char dieSideData[3];
    struct boggleDieSideNode *nextSide;
};
//creating the function prototypes.
void input(struct boggleDataNode *head);
void addBoggleData(struct boggleDataNode *head, char data);
void displayBoggleData(struct boggleDataNode *head);

//main function
int main()
{
    //creating a var to use as a counter
    int counter = 0;
    struct boggleDataNode *head1 = NULL;
    struct boggleDieSideNode *head2 = NULL;
    //calls the function that reads data intp a file.
    input(head1);
    //displays the data from rhe file
    displayBoggleData(head1);
    //displays the die sides
    displayDieSide(head2);

    //creates a for loop to create the die sides.
    int side, die;
    for(die =0; die<16; die++){
       //resets the entire link list.
        head2 = NULL;
        for(side = 0; side<6; side++){
                //adds the data to the die side (NOT COMPLETE!)
            addDieSideNode(head2, head1);

        }
    }



}
//this function will handle user imput
void input (struct boggleDataNode *head1)
{
    //create  data size as well as bounds
    char fileData[3];
    int bound = 96;
    //file pointer and file info
     FILE *ips;
     ips = fopen("data.txt", "r");
      if (ips == NULL)
        printf("Please check file!\n");
     else {
            //for loop to scan through file, and retrive the letters
            int i;
            for(i=0; i<bound; i++)
              fscanf(ips, "%s", fileData);
              addBoggleData(head1, fileData);
              }
     //closes the file system
              close(ips);
}
void addBoggleData(struct boggleDataNode *head, char data)
{
    //create a helper and temp
    struct boggleDataNode *temp,*helper;
    //allocate memory (error here)
    temp =(struct boggleDataNode *)malloc(sizeof(struct node));
    //this sets the data for my temp variable
     strcpy(temp.data, data);
    if(head==NULL){
        //sets the first portion of the link list
        head = temp;
        temp.nextData = NULL;
//append function 1
}else
    {
        helper = head;

        //(line 70) I dont even know what I am doing. I dont know why temp points to the head, and why I am setting head = temp.
       while(helper.nextData != NULL){

        helper = helper.nextData;

       }
       helper.nextData = temp;

    }



}
//display function
void displayBoggleData(struct boggleDataNode *head){
    //creates a helper var to loop through the link list.
    struct boggleDataNode *helper;
     helper-head;
     if(helper==NULL){
        return;
     }
     //counter to keep track of the data value
    int counter=0;
 while(helper != NULL){
        //prints data
    printf(" Data Value %d %s", &counter, helper.data);
    counter++;
 //moves on
    helper = helper.nextData;
 }


}
//something that I might never figure out.
void addBoggleSide()
// Incomplete
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
john jil
  • 41
  • 7

1 Answers1

1

First and foremost, don't cast the return value of malloc() in C. Then,

but I dont know where to start.

Let me help you with that by giving a couple of pointers (pun intended).

Coming to issues, in your code,

  1. temp is of type struct boggleDataNode *, so the memory allocation statement

     temp =(struct boggleDataNode *)malloc(sizeof(struct node));
    

    should be

     temp = malloc(sizeof(struct boggleDataNode ));
    

    or rather, a better and robust version

    temp = malloc(sizeof*temp);   //yes, sizeof operator only needs () 
                                  //    when using a datatype as operand
    

    The last statement is independent and immune to the changes of the type for temp.

  2. temp being a pointer type,

     temp.nextData = NULL;
    

    should be

    temp->nextData = NULL;
    

Now, please check and correct all similar issues (if any), in your code.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • I think `temp = malloc(sizeof *temp);` is even better than `temp = malloc(sizeof(struct boggleDataNode ));`. – mch Jun 26 '15 at 20:21
  • @mch I kept the original syntax to point out the mistake. Nevertheless, valuable suggestion, I'll add that to the answer. Thanks. :-) – Sourav Ghosh Jun 26 '15 at 20:23
  • 1
    WOW thanks! I seriously cant thank you guys enough. My program actually ran! My professor and TA said that the period made no difference, Thats interesting! – john jil Jun 26 '15 at 20:36
  • now my file isnt being read? I put it in my debug folder, could there be an issue with my code? – john jil Jun 26 '15 at 20:46
  • 1
    @johnjil Check the return value of `fscanf()` to ensure a successful read. Try to use a debugger like `gdb` to stop at break points to find the possible error. If issue persists, [as per the community rules](http://meta.stackexchange.com/a/43485/244062), you need to ask a new question with all the relevant details. :-) – Sourav Ghosh Jun 26 '15 at 20:55