debugging help please in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmprince01
    New Member
    • Sep 2006
    • 11

    #1

    debugging help please in C

    I have three files list.c list.h driver.c. they dont compile properly (they are to be compiled together). i can't find my assignment specs right now, i'll try to post them soon. but maybe someone can make sense of this w/o my instructions?

    Code:
     list.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "list.h"
    
    #define LISTSZ(a) (((a)-1)*sizeof(char*)+sizeof(struct list))
    
    struct list{
    	int size;
    	int maxsz;
    	char *string[1];
    };
    
    struct list* startlist (int numstrings)
    {	
    	struct list *q;
    	q=malloc(LISTSIZE(numstrings));
    
    	if (q==NULL)
    	{
    		perror("malloc error\n");
    		exit(-1);
    	}
    q-> size=0;
    q-> maxsz= numstrings;
    
    return 	q;
    }
    
    
    struct list* insrtstr(char sizestring[], struct list *p){
    	char *srcptr, *destptr;
    	struct list *q;
    	if(p->size == p->maxsz){
    		p-> maxsz+= INCRSZ;
    		q=realloc(p, LISTSIZE(p->maxsz));
    
    		if(q==NULL)
    		{
    			perror("realloc error\n");
    			exit (-1);
    		}
    		p=q;
    	}
    	p->string[p->size++]= (char*)strdup(sizestring);
    		return p;
    }
    /*/void sortlist(struct list *p){
    //	sizeone++;
    //	sizetwo++; */
    //

    Code:
     list.h
    #ifndef _LIST_H_
    #define _LIST_H_
    
    #define INITSZ 7
    #define INCRSZ 10
    
    struct list;
    
    	struct list* startlist(int);
    	struct list* insrtstr(char [], struct list*);
    
    	void sortlist(struct list *);
    	struct list* rmvstr(int, struct list *);
    
    	void printlist(struct list *);
    	void freelist(struct list *);
    
    	int strCompare (const void *, const void *);
    
    #endif

    Code:
     driver.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include  "list.h"
    
    #define MAX_STRING 1024
    #define REMOVE_POSITION 4
    
    int main(int argc, char *argv[]){
    	FILE *in;
    	char *str;
    	struct list *anotherList;
    
    	str=malloc (MAX_STRING);
    	in=fopen(argv[1], "r");
    
    	anotherList=startlist(INITSZ);
    	while(fgets(str, MAX_STRING, in)!= NULL){
    		anotherList=insrtstr(str, anotherList);
    	}
    
    	sortlist(anotherList);
    
    	anotherList=rmvstr(REMOVE_POSITION, anotherList);
    	printlist(anotherList);
    
    	fclose(in);
    	free(str);
    
    	freelist(anotherList);
    	return EXIT_SUCCESS;
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by jmprince01
    they dont compile properly
    Then you should have posted the compile errors/warnings. I can see anything from a quick glance at the code.

    Comment

    • jmprince01
      New Member
      • Sep 2006
      • 11

      #3
      here are my compiler errors

      Code:
      main203{jprince}9: gcc driver.c list.c list.h -o driver
      driver.c:4:19: list.h: No such file or directory
      driver.c: In function `main':
      driver.c:17: error: `INITSZ' undeclared (first use in this function)
      driver.c:17: error: (Each undeclared identifier is reported only once
      driver.c:17: error: for each function it appears in.)
      driver.c:17: warning: assignment makes pointer from integer without a cast
      driver.c:19: warning: assignment makes pointer from integer without a cast
      driver.c:24: warning: assignment makes pointer from integer without a cast
      list.c:4:18: list.h: No such file or directory
      list.c: In function `insrtstr':
      list.c:35: error: `INCRSZ' undeclared (first use in this function)
      list.c:35: error: (Each undeclared identifier is reported only once
      list.c:35: error: for each function it appears in.)

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        main203{jprince }9: gcc driver.c list.c list.h -o driver

        driver.c:4:19: list.h: No such file or directory

        The compiler can not find list.h, is it saved in the same directory as the .c files? If not you will have to add an include path to the compiler command line.

        After this all bets are off, it is trying to compile the file without the proper headers included.

        list.c:4:18: list.h: No such file or directory

        Same again here.

        Comment

        • jmprince01
          New Member
          • Sep 2006
          • 11

          #5
          still working on this. i changed the <list.h> to "list.h" which gave me a bunch new errors. they're all "undefined reference to 'some function'", but all my functions are defined in list.h. (yes, list.h is in the correct director with the other two files) so how do i fix the "undefined reference" problem? i am honestly not getting this.
          Here are my new errors:

          Code:
          main203{jprince}6: gcc driver.c list.c list.h -o driver
          /tmp/ccsWJzVQ.o: In function `main':
          driver.c (.text+0x91) undefined reference to `sortlist'
          driver.c (.text+0xa1) undefined reference to `rmvstr'
          driver.c (.text+0xb2) undefined reference to `printlist'
          driver.c (.text+0xdc) undefined reference to `freelist'
          /tmp/ccnS5eoU.o: In function `startlist':
          list.c (.text+0xd) undefined reference to `LISTSIZE'
          /tmp/ccnS5eoU.o: In function `insrtstr':
          list.c (.text+0x88) undefined reference to `LISTSIZE'
          collect2: ld returned 1 exit status

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by jmprince01
            but all my functions are defined in list.h
            No. All your functions are declared in list.h.

            sortlist, rmvstr, printlist, freelist are not defined anywhere hence the errors.

            I function declaration is where you state what the return type of the function is as well as the number of parameters it takes as well as there types, it is followed by a semi colon and has no code block.

            Code:
            int Add(int op1, int op2); // Declaration of Add
            A definition as well as having the return type and number of types of parameters also has the code block for the function giving the code to be executed when the function is called, it has no semi colon.

            Code:
            int Add(int op1, int op2) // Definition of Add
            {
                return op1+op2;
            }
            You have link errors, undefined functions because you have declared the functions and tried to call them but not defined them. This makes a lot of sense if you think about it because where you have called the function you are basically saying run the code for the function printlist (for instance) but you have not associated any code with printlist so the linker can't do it.



            Note: LISTSIZE is actually defined LISTSZ

            Comment

            Working...