944,009 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 545
  • C RSS
Nov 9th, 2009
0

pointers and malloc help needed

Expand Post »
So I have an assignment that I have been attempting for 2 weeks now and the class has been given extensions twice now. Everyone seems to be struggling with this part of the assignment. The assignment is to do K&R Exercise 5-13, unix tail command.
Instructions: "The program takes lines from standard input and keeps the last n of them in memory as it goes through standard input. When it gets to an EOF, it prints the last n lines out. You may assume n is less than 2000 and each individual line is not longer than 1000 including the newline and the null terminator.

Use two source files plus a header file for this, just to show you know how to make multiple source files work together.

tail.c:
interprets the command line argument.
Calls init_lineholder(int nlines) with the numebr from the command line.
Does a loop calling getline and insert_line(char *line)
when getline returns 0 (indicating EOF on stdin), it calls print_lines().

lineholder.c
contains a static array of pointers for lines.
Implements init_lineholder, insert_line, and print_lines.
Init_lineholder initializes the "first" slot and related variables.
Insert_line adds a line to the array.
It must allocate memory for the new line.
It must free the memory for a line no longer needed, if any.
Print_lines prints the lines in the array and frees the memory used for them.

lineholder.h: Just has prototypes for the three calls with appropriate comments explaining what they do for the caller.


Sources:
  1. blade71(148)% cat lineholder.h
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /* This header file contains the prototypes of the three functions
  8.   in lineholder.c */
  9.  
  10. int init_lineholder(int nlines);
  11.  
  12. int insert_line(char *line);
  13.  
  14. void print_lines();
lineholder.c is correct, however I need to implement memory allocation.
  1. blade71(160)% cat lineholder.c
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. static char lines;
  8. lines = *(*lines + MAXLEN) + MAXLINE);
  9. static char (*first) [MAXLEN], (*last) [MAXLEN], (*end) [MAXLEN];
  10. static int wrapped;
  11.  
  12. void init_lineholder(int lines)
  13. {
  14. if (nlines > MAXLINE)
  15. nlines = MAXLINE;
  16. first = last = lines;
  17. end = lines + nlines;
  18. wrapped = 0;
  19. }
  20.  
  21. void insert_line(char *line)
  22. {
  23. strcpy(*last++, line);
  24.  
  25. if (wrapped)
  26. first = (++first >= end) ?
  27. lines : first;
  28. if (last >= end)
  29. {
  30. last = lines;
  31. wrapped = 1;
  32. }
  33. }
  34.  
  35. void print_lines(void)
  36. {
  37. do {
  38. if (*first != NULL)
  39. printf("%s", *first);
  40.  
  41. first = (++first >= end) ?
  42. lines : first;
  43. }
  44. }
Biggest mess of them all....:

  1. blade71(188)% cat tail.c
  2. #include <ctype.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define MAXLINE 2000
  8. #define maxlen 1000
  9.  
  10. int getline(char *line, int max);
  11. int main (int argc, char *argv[])
  12. {
  13. int nlines = 0;
  14. char line[MAXLINE];
  15.  
  16. if (argc != 2)
  17. printf("Too few of arguments");
  18. else
  19. {
  20. while (getline(line, MAXLINE) > 0)
  21. {
  22. nlines++;
  23. }
  24. return nlines;
  25. print_lines();
  26. /* Calls init_lineholder(int nlines) with nlines */
  27. /* Does loop callinggetline and insert_line(char*line) */
  28. /* When getline returns 0, call print_lines() */
  29. }
  30. }
  31. int getline(char *line, int max)
  32. {
  33. int nch = 0;
  34. int c;
  35. max = max - 1; /* leave room for '\0' */
  36.  
  37. #ifndef FGETLINE
  38. while((c = getchar()) != EOF)
  39. #else
  40. while((c = getc(fp)) != EOF)
  41. #endif
  42. {
  43. if(c == '\n')
  44. break;
  45. if(nch < max)
  46. {
  47. line[nch] = c;
  48. nch = nch + 1;
  49. }
  50. }
  51. if(c == EOF && nch == 0)
  52. return EOF;
  53. line[nch] = '\0';
  54. return nch;
  55. }
Last edited by Ineedhelpplz; Nov 9th, 2009 at 12:24 pm. Reason: fixed
Similar Threads
Reputation Points: 3
Solved Threads: 1
Junior Poster in Training
Ineedhelpplz is offline Offline
57 posts
since Feb 2009
Nov 10th, 2009
-1
Re: pointers and malloc help needed
83 and no response....does it look right? can someone at least point out what I'm doing wrong. Do my algorithms make sense?
Reputation Points: 3
Solved Threads: 1
Junior Poster in Training
Ineedhelpplz is offline Offline
57 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Reverse of number
Next Thread in C Forum Timeline: Description of the interpolation search algorithm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC