(*pointers) & Command Line problem...?

This is a discussion on (*pointers) & Command Line problem...? within the C Programming forums, part of the General Programming Boards category; It seems like this code is one-step away from working.. but down (in lines 37-44) *lineptr seems to be 'losing' ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    8

    (*pointers) & Command Line problem...?

    It seems like this code is one-step away from working.. but down (in lines 37-44) *lineptr seems to be 'losing' its value. i.e. it successfully prints in the printf on line37, then seems empty in the printf on line42)

    Code:
    #include <stdio.h>
    #define N 10 /*default lines of input to print*/
    #define MAXLINES 5000
    char *lineptr[MAXLINES];/*pointers to text lines*/
    void writelines(char *lineptr[], int nlines);
    
    main(int argc, char *argv[]) {/* prints the last n ("tail -n") lines of input with command line option*/
    	int n=0,nlines;/*number of input lines read*/
    	if (*++argv!='\0' && **argv=='-'){
    		n=(atoi(*argv))*-1;
    	}
    	if ((nlines=readlines(lineptr,MAXLINES,n)) >= 0) {
    		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); /* return pointer to n characters */
    
    int readlines(char *lineptr[], int maxlines, int n) { /*readlines: read input lines into array*/
    	int len, nlines,j=0; 
    	char *p, line[MAXLEN];
    	nlines=0;
    	while ((len=getline(line,MAXLEN))>1)
    		if (nlines >= maxlines || (p = alloc(len)) == NULL)
    			return -1;
    		else {
    			line[len-1]='\0'; /*delete newline*/
    			strcpy(p,line);
    			*lineptr=line;
                                                    printf("%s", *lineptr++); /*something seems wrong here..*/
    			nlines++;
    		}
    	while (j<(n>0? n : N)){/*now move pointer back (n or (default) N) places for printing*/
    		*lineptr--;printf("%s", *lineptr); /*now *lineptr is empty ?*/
    		j++;
    	}
    	printf("%s", *lineptr);
    	return nlines;
    }
    
    /* getline: get line into s, return length */
    int getline(char s[], int lim){
    	int c, i=0;
    	while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
    		s[i++] = c;
    	if (c == '\n')
    		s[i++] = c;
    	s[i] = '\0';
    	return i;
    }
    
    #define ALLOCSIZE 10000 /* size of available space */
    static char allocbuf[ALLOCSIZE]; /* storage for alloc */
    static char *allocp = allocbuf; /* next free position */
    
    char *alloc(int n)/* return pointer to n characters */
    {
    if (allocbuf + ALLOCSIZE - allocp >= n) { /* it fits */
    allocp += n;
    return allocp - n; /* old p */
    } else /* not enough room */
    return 0;
    }
    
    /*writelines: write output lines */
    void writelines(char *lineptr[], int nlines) {
    	while(*lineptr != '\0') 
    		printf("%s\n test",*++lineptr);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's really awesome how you told us what line number it was on, but didn't bother numbering your lines, so we're all supposed to manually count the lines. That's swell. Next time, consider highlighting what you are talking about.
    Code:
    /*writelines: write output lines */
    void writelines(char *lineptr[], int nlines) {
    	while(*lineptr != '\0') 
    		printf("%s\n test",*++lineptr);
    }
    You are passing an array of pointers. You shouldn't be controlling your loop this way, for one because you aren't actually using nlines at all. What's the point of passing two arguments if you only use one of them?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    This code was originally using arrays/indexes, but the task was to convert it soly to the use of pointers - hence some strange usages (which may be wrong).

    But the problem seems to be in the (now highlighted) code. in the first highlight, *lineptr has values, but then (in the second highlight), it is empty. (I have tried printf's after each line to see what is in there).

    Here are the highlighted lines:

    Code:
    int readlines(char *lineptr[], int maxlines, int n) { /*readlines: read input lines into array*/
    	int len, nlines,j=0; 
    	char *p, line[MAXLEN];
    	nlines=0;
    	while ((len=getline(line,MAXLEN))>1)
    		if (nlines >= maxlines || (p = alloc(len)) == NULL)
    			return -1;
    		else {
    			line[len-1]='\0'; /*delete newline*/
    			strcpy(p,line);
    			*lineptr=line;
                                                    printf("%s", *lineptr++); /* *lineptr has values here*/			nlines++;
    		}
    	while (j<(n>0? n : N)){/*now move pointer back (n or (default) N) places for printing*/
    		*lineptr--;printf("%s", *lineptr); /*now *lineptr is empty ?*/		j++;
    	}

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    			strcpy(p,line);
    			*lineptr=line;
    You are making it point at an array that will go out of scope, and whose contents keep getting changed. You should be making it point at p.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I thought the code seems familiar. It's from K&R!
    In writeline func you shouldn't be comparing with \0.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    8
    Yes it is from the KR text.

    I see now that 'line' was being used wrong as an array, causing the problem. Thanks to quzah, as pointing to 'p' was what the code needed to do and now works correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21