Thread: debug problem --> chapter 5.6 K&R

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Angry debug problem --> chapter 5.6 K&R

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MAXLINES 5000 /* max #lines to be sorted */
    char *lineptr[MAXLINES]; /* pointers to text lines */
    int readlines(char *lineptr[], int nlines);
    void writelines(char *lineptr[], int nlines);
    void qsort(char *lineptr[], int left, int right);
    /* sort input lines */
    main()
    {
    	int nlines; /* number of input lines read */
    	if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
    		qsort(lineptr, 0, nlines-1);
    		writelines(lineptr, nlines);
    		return 0;
    	} else {
    		printf("error: input too big to sort\n");
    		return 1;
    	}
    }
    #define MAXLEN 1000 /* max length of any input line */
    int getline(char *, int);
    char *alloc(int);
    /* readlines: read input lines */
    int readlines(char *lineptr[], int maxlines)
    {
    	int len, nlines;
    	char *p, line[MAXLEN];
    	nlines = 0;
    	while ((len = getline(line, MAXLEN)) > 0)
    		if (nlines >= maxlines || (p = alloc(len)) == NULL)
    			return -1;
    		else {
    			line[len-1] = '\0'; /* delete newline */
    			strcpy(p, line);
    			lineptr[nlines++] = p;
    		}
    	return nlines;
    }
    /* writelines: write output lines */
    void writelines(char *lineptr[], int nlines)
    {
    	int i;
    	for (i = 0; i < nlines; i++)
    		printf("%s\n", lineptr[i]);
    }
    Could you please help me out why I cannot debug? How to do so...

    I get these two errors:
    "_alloc", referenced from:
    "_getline", referenced from:

    Looking forward to your answers!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    As far as I can tell, it looks like you don't have alloc() or getline() anywhere in your source. But you call these functions, so they need to be included as a preprocessor directive or directly in the source.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    done! thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static vector<...> in class problem.
    By alkis_y3k in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2003, 04:13 PM
  2. Debug Problem
    By drdroid in forum C++ Programming
    Replies: 10
    Last Post: 03-22-2002, 09:05 PM
  3. Help, Debug Problem
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2002, 03:50 PM
  4. Visual C++ 6.0 Debug Problem
    By Bradley in forum C++ Programming
    Replies: 0
    Last Post: 11-20-2001, 07:31 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM