Thread: Vertical from Command Line

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Vertical from Command Line

    Im trying to get the operands from argv to print vertically. for some reason, after it gets to the end of an operand, it starts to print the next operand.

    Code:
    int col= 0, maxlength = 0, row= 0;
    	for(col = optind; col < argc; col++){
    		if(maxlength < strlen(argv[col])){
    			maxlength = strlen(argv[col]);
            }
        }
           char c;
    	for(row = 0; row < maxlength; row++){
    		for(col = optind; col < argc; col++){
    			if (row < strlen(argv[col])){
    			   	   c = argv[col][row];
    			     argv[col][row]=c;
    		        putchar(argv[col][row]);}
    			else{
    				argv[col][row]=' ';
    				putchar(' ');}
            }
    			putchar('\n');
    	}
        }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    			if (row < strlen(argv[col])){
    			   	   c = argv[col][row];
    			     argv[col][row]=c;
    		        putchar(argv[col][row]);}
    			else{
    				argv[col][row]=' ';
    				putchar(' ');}
            }
    Why are you assigning argv[bla] to c then back again?

    Next, if you assign ' ' to argv[bla] and bla oversteps the array, that's bad.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Trumpman06 View Post
    Im trying to get the operands from argv to print vertically. for some reason, after it gets to the end of an operand, it starts to print the next operand.
    Because you do this:

    Code:
    argv[col][row]=' ';
    That overwrites the last character in argv[col], which would have been '\0'. Since **argv is contained sequentially in memory, you have now connected argv[col] to argv[col+1] with a space.

    If you add some printf's, I am sure you will notice the strlen on the next pass has increased to include the next string in the array (and possibly more, if they have already had their null terminator overwritten).

    Even if this had not happened, it is a very bad idea to continue writing spaces to an array when you know it's allocated space has been exceeded.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  2. Fastest way to draw a vertical line
    By CrazyNorman in forum Game Programming
    Replies: 7
    Last Post: 10-17-2006, 09:59 AM
  3. Using C++ to create a Vertical Shooter
    By mindofpoison in forum Game Programming
    Replies: 5
    Last Post: 01-02-2006, 03:42 PM
  4. BitBlt and vertical retrace
    By Magos in forum Windows Programming
    Replies: 1
    Last Post: 06-15-2002, 02:32 PM
  5. wait for vertical retrace-crashes laptop
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-13-2002, 05:00 AM