pointers and malloc help needed

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 57
Reputation: Ineedhelpplz has a little shameless behaviour in the past 
Solved Threads: 1
Ineedhelpplz Ineedhelpplz is offline Offline
Junior Poster in Training

pointers and malloc help needed

 
0
  #1
Nov 9th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 57
Reputation: Ineedhelpplz has a little shameless behaviour in the past 
Solved Threads: 1
Ineedhelpplz Ineedhelpplz is offline Offline
Junior Poster in Training
 
-1
  #2
Nov 10th, 2009
83 and no response....does it look right? can someone at least point out what I'm doing wrong. Do my algorithms make sense?
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 253 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC