Problem with struct arrays...

This is a discussion on Problem with struct arrays... within the C Programming forums, part of the General Programming Boards category; First of all, I'd like to thank everyone who helped me out on my last post. It helped me get ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    Problem with '->' operator...

    First of all, I'd like to thank everyone who helped me out on my last post. It helped me get to where I am right now. The problem I'm having is that my debugger (I'm using devc++) is saying that the operator '->' is not valid, and I have no idea why. I marked in my code where the problem is happening.

    The point of my program is to copy in comic books from a file, sort them, then print them. Here's the code so far.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct comic
    {
           char title[100];
           char issue[100];
           int issuenum;
           //struct comic *next;
    };
    
    //void assign(struct comic **comics, char title[100], char issue[100], int issuenum);
    
    void mergesort(struct comic *comics, int len);
    void merge(struct comic *comicsc, struct comic *leftc, int leftlen, struct comic *rightc, int rightlen);
    
    main()
    {
          char filename[50], title[100], issue[100];
          char *p;
          char uscore = '_';
          char blank = ' ';
          int issuenum, numcomics, count = 0, cnt = 0, past = 0, pcnt = 0, tempcnt = 0;
          struct comic *thecomics;
          struct comic *temp;
          FILE *f1;
          
          printf("--------------------------------------------------------------------------------------");
          printf("Welcome to the comic book sorter!\n");
          printf("Please enter in the name of the file containing the comic books: ");
          //fflush();
          fgets(filename, sizeof(filename), stdin);
          
          //Removes the \n that ENTER adds into the filename input
          if((p = strchr(filename, '\n')) != NULL)
          {
                *p = '\0';
          }
          
           //Opens the file
           f1 = fopen(filename, "r");
           //fflush();
           
           if(f1 != NULL)
           {
                 fscanf(f1, "%d", &numcomics);
                 system("pause");
                 
                 thecomics = (struct comic*)calloc(numcomics, sizeof(struct comic));
                 
                 do
                 {
                        fscanf(f1, "%s %d %s", title, &issuenum, issue);
                        cnt = 0;
                        //Checks for the _ in the string and replaces it with a "space"
                        do
                        {
                             if(title[cnt] == uscore)
                             {
                                     title[cnt] = blank;
                             }
                             cnt++;
                        }while(cnt < 100);
                                   
                        //Checks for the _ in the string and replaces it with a "space"
                        cnt = 0;
                        do
                        {
                             if(issue[cnt] == uscore)
                             {
                                     issue[cnt] = blank;
                             }
                             cnt++;
                        }while(cnt < 100);
                        
    ////THE ERROR STARTS HERE
                        strcpy(thecomics[count]->title, title);
                        strcpy(thecomics[count]->issue, issue);
                        thecomics[count]->issuenum = issuenum;
                        
                        count++;
                 }while(count < numcomics);
                 
                 //Sorts by title and then mergesorts by comic number and prints
                 count = 0;
                 do
                 {
                       //Tests to see if the title has already been run
                       pcnt = count - 1;
                        while(pcnt >= 0)
                        {
                            if(strcmp(thecomics[count]->title, thecomics[pcnt]->title) == 0)
                            {
                                  past = 1;
                            }
                            pcnt--;
                        }
                        
                        
                        if(past != 1)
                        {
                                tempcnt = 1;
                                cnt = count +1;
                                do
                                {
                                    if(strcmp(thecomics[count]->title, thecomics[cnt]->title) == 0)
                                    {
                                         temp[0] = thecomics[count];
                                         temp[tempcnt] = thecomics[cnt];
                                         tempcnt++;
                                    }
                                    cnt++;
                                }while(cnt < numcomics);
                                
                                mergesort(temp, tempcnt);
                        
                                cnt = 0;
                                printf("%s\n", temp->title);
                                while(cnt <= tempcnt)
                                {
                                          printf("\t%d. %s\n", temp[cnt]->issuenum, temp[cnt]->issue);
                                          cnt++;
                                } 
                        }//End IF
                        
                        count++;
                 }while(count < numcomics);
                                                           
                 
           }//Close if
           else
           {
               printf("There is nothing in the file or it doesn't exist!\n");
           }//Close Else
           system("pause");
    }//Close main
    
    ///////////////////////////////////////////////////////////////////////////
    /////////In Case I wanted to use linked lists//////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    /*void assign(struct comic **comics, char title[100], char issue[100], int issuenum)
    {
         struct comic *count = *comics;
         struct comic *start;
         
         if(count == NULL)
         {
              strcpy(count->title, title);
              strcpy(count->issue, issue);
              count->issuenum = issuenum;
              count->next = NULL;
         }
         else
         {
              start = (struct comic*)malloc(sizeof(struct comic));
         
              strcpy(start->title, title);
              strcpy(start->issue, issue);
              start->issuenum = issuenum;
              start->next = NULL;
         
              while(count->next != NULL)
              {
                   count = count->next;
              }
         
              count->next = start;
              
         }
    }*/
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    void merge(struct comic *comicsc, struct comic *leftc, int leftlen, struct comic *rightc, int rightlen)
    {
        int comicsnum = 0;
    	int leftnum = 0;
    	int rightnum = 0;
    
        // As long as both arrays have more integers, keep merging
    	while(leftnum < leftlen && rightnum < rightlen)
    	{
            // If the smallest remaining integer in the left half is smaller than
            // the smallest remaining integer in the right half, put the integer 
            // from the left half into the final array first
    		if(leftc[leftnum]->issuenum <= rightc[rightnum]->issuenum)
    		{
    			comicsc[comicsnum] = leftc[leftnum];
    			comicsnum++;
    			leftnum++;
    		}
    		// Otherwise, go with the right half
    		else
    		{
    			comicsc[comicsnum] = rightc[rightnum];
    			comicsnum++;
    			rightnum++;
    		}
    	}
    	
    	// If there aren't any integers left in the right half, that means there
        // must still be some in the left half. Copy the remaining integers from the
        // left half of the array.
    	if(rightnum >= rightlen) 
    	{
    		while(leftnum < leftlen)
    		{
    			comicsc[comicsnum] = leftc[leftnum];
    			comicsnum++;
    			leftnum++;
    		}
    	}
    	// Otherwise, there are still integers in the right half, so copy those
    	else
    	{
    		while(rightnum < rightlen)
    		{
    			comicsc[comicsnum] = rightc[rightnum];
    			comicsnum++;
    			rightnum++;
    		}
    	}
    }//End merge
    
    
    
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    void mergesort(struct comic *comics, int len)
    {
         struct comic *left;
         struct comic *right;
         int ct;
         
         
         if(len <= 1) 
         {
                return;
         }
         
         left = (struct comic*)calloc(len/2, sizeof(struct comic));
         for(ct = 0; ct < len/2; ct++)
         {
               left[ct] = comics[ct];
         }
         mergesort(left, len/2);
         
         right = (struct comic*)calloc(len - len/2, sizeof(struct comic));
    	 for(ct = len/2; ct<len; ct++)
    	 {
    		   right[ct - len/2] = comics[ct];
    	 }
    	 mergesort(right, len - len/2);
    	 
    	 merge(comics, left, len/2, right, len-len/2);
    	 
    	 free(left);
    	 free(right);
    }//End mergesort
    
    
    //////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////
    Anyone know whats going on?
    Last edited by Bizmark; 03-27-2008 at 07:13 PM. Reason: Not explicit with problem...

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    I updated all the '->' with a '.' and that seemed to fix the problem. Now the problem is when my program gets down to the mergesort command it throws a runtime error. Anyone see anything wrong with the way my pointers are setup in my functions?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see anything just by looking. What kind of debug information do you have? Have you at least put some printf's to see where you are when it all goes pear-shaped?

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Quote Originally Posted by tabstop View Post
    I don't see anything just by looking. What kind of debug information do you have? Have you at least put some printf's to see where you are when it all goes pear-shaped?
    Yeah i fixed that problem by replacing all the '->' with '.' Now the problem is that the functions merge and mergesort aren't working, it pops an error when it calls those. I have no idea what the problem is with that. Do you see anything wrong with them?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Bizmark View Post
    Yeah i fixed that problem by replacing all the '->' with '.' Now the problem is that the functions merge and mergesort aren't working, it pops an error when it calls those. I have no idea what the problem is with that. Do you see anything wrong with them?
    This is the question I was responding to. Where do you get the errors; how many recursive steps does it make; is it the free, or a copy, that causes the problem? How large is your file, and can your stack handle all the recursion? If you don't know, put some print statements in your code so you can see where things go wrong.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Quote Originally Posted by tabstop View Post
    This is the question I was responding to. Where do you get the errors; how many recursive steps does it make; is it the free, or a copy, that causes the problem? How large is your file, and can your stack handle all the recursion? If you don't know, put some print statements in your code so you can see where things go wrong.
    //Ok I did that and it seems the problem happens right when mergesort is called, because it doesn't even get to the first line of code.

    Sorry i'll go ahead and comment that first line there =). It gets to the part where it starts recursively calling and then screws up.
    Last edited by Bizmark; 03-27-2008 at 08:52 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 07:45 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21