| | |
pointers and malloc help needed
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 57
Reputation:
Solved Threads: 1
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:
lineholder.c is correct, however I need to implement memory allocation.
Biggest mess of them all....:
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:
C Syntax (Toggle Plain Text)
blade71(148)% cat lineholder.h #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> /* This header file contains the prototypes of the three functions in lineholder.c */ int init_lineholder(int nlines); int insert_line(char *line); void print_lines();
C Syntax (Toggle Plain Text)
blade71(160)% cat lineholder.c #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> static char lines; lines = *(*lines + MAXLEN) + MAXLINE); static char (*first) [MAXLEN], (*last) [MAXLEN], (*end) [MAXLEN]; static int wrapped; void init_lineholder(int lines) { if (nlines > MAXLINE) nlines = MAXLINE; first = last = lines; end = lines + nlines; wrapped = 0; } void insert_line(char *line) { strcpy(*last++, line); if (wrapped) first = (++first >= end) ? lines : first; if (last >= end) { last = lines; wrapped = 1; } } void print_lines(void) { do { if (*first != NULL) printf("%s", *first); first = (++first >= end) ? lines : first; } }
C Syntax (Toggle Plain Text)
blade71(188)% cat tail.c #include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXLINE 2000 #define maxlen 1000 int getline(char *line, int max); int main (int argc, char *argv[]) { int nlines = 0; char line[MAXLINE]; if (argc != 2) printf("Too few of arguments"); else { while (getline(line, MAXLINE) > 0) { nlines++; } return nlines; print_lines(); /* Calls init_lineholder(int nlines) with nlines */ /* Does loop callinggetline and insert_line(char*line) */ /* When getline returns 0, call print_lines() */ } } int getline(char *line, int max) { int nch = 0; int c; max = max - 1; /* leave room for '\0' */ #ifndef FGETLINE while((c = getchar()) != EOF) #else while((c = getc(fp)) != EOF) #endif { if(c == '\n') break; if(nch < max) { line[nch] = c; nch = nch + 1; } } if(c == EOF && nch == 0) return EOF; line[nch] = '\0'; return nch; }
Last edited by Ineedhelpplz; Nov 9th, 2009 at 12:24 pm. Reason: fixed
![]() |
Similar Threads
- Vectors: Gaming Simulation (C++)
- Need help implementing a gradebook using dynamic memory (C++)
- Smart Pointers assignment (C++)
- Pointers (archived tutorial) (C++)
- reasons why malloc fails? (C)
- FindNextFile() problem (C++)
- array of structures within a struct (C++)
- Pointers (C++)
- Pointers (Part II) (C)
Other Threads in the C Forum
- Previous Thread: Reverse of number
- Next Thread: Description of the interpolation search algorithm
Views: 253 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student systemcall test testautomation unix user variable visualstudio voidmain() wab win32 win32api windows.h





