Results 1 to 3 of 3

Thread: [Resolved] Arrays + Pointers in C

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Resolved [Resolved] Arrays + Pointers in C

    I'm trying to write some code that takes input lines and then outputs the last N lines. But all my code is doing is outputting the last line N times. Here's the code:

    Code:
    #include <stdio.h>
    
    int N = 4;
    const int MAX_LENGTH = 50;
    
    int main(int argc, char *argv[]) {
    
    	char **lines;
    	char *currLine = (char *) calloc(MAX_LENGTH, sizeof (char));
    
    	int start = 0;
    	int current = 0;
    	int i = 0;
    
    	if (argc > 0 && isdigit(argv[1])) {
    		N = argv[1] - '0';
    
    		printf("%d\n", N);
    	}
    
    	lines = (char **) calloc (N, sizeof (char *));
    	for (i = 0; i < N; i++)
    		lines[i] = calloc(MAX_LENGTH, sizeof (char));
    
    
    	fgets (currLine, MAX_LENGTH, stdin);
    	while (! feof (stdin)) {
    		*(currLine + strlen(currLine) - 1) = 0;
    
    		if (current == N) {
    			current = 0;
    			start = 1;
    		} else if (start != 0) {
    			if (start == N - 1) {
    				start = 0;
    			} else {
    				start++;
    			}
    		}
    
    		*(lines + MAX_LENGTH * current) = currLine;
    
    		printf("%s\n", *(lines + MAX_LENGTH * current));
    
    		current++;
    
            fgets (currLine, MAX_LENGTH, stdin);
    	}
    
    	// output lines:
    	for (i = start; i < N; i++) {
    		printf("f[%d]: %s\n", i, *(lines + MAX_LENGTH * i));
    	}
    
    	if (start != 0) {
    		for (i = 0; i < start; i++) {
    			printf("f[%d]: %s\n", i, *(lines + MAX_LENGTH * i));
    		}
    	}
    
    	free(lines);
    	free(currLine);
    
    	return 0;
    }
    So if the input is:
    Line #1
    Line #2
    Line #3
    Line #4

    and N = 2, then the output should be:
    Line #3
    Line #4

    What am I screwing up?
    Last edited by The Hobo; Oct 9th, 2005 at 02:01 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Arrays + Pointers in C

    You need to use strcpy to copy the lines to their buffers.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: Arrays + Pointers in C

    Ah, thanks CB.
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width