Thread: Program segfaults passing information from the command line

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116

    Program segfaults passing information from the command line

    I have another problem with command line arguments.
    But this time I'm mostly convinced that it's how I've set it up.
    This program takes two numbers from the command line. The first and presumably lesser number is determined to be either even or odd. If the first number is even it divides the number by 2 and if the number is odd the number is multiplied by 3 and 1 then added to it and increments a counter every time either one of these events happens until the value is 1. The smaller number is then incremented by 1 and the process starts over again until the smaller number is equal to the second presumably greater number.

    This is the code that I have, it compiles OK, but I have a logical error some where causing a segmentation fault and I believe it's how I'm using the command line information (or not using it as the case maybe).

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	int origi;
    	char i, j;
    	origi = i; //hold the original value of *i
    	int count;
    	int temp; //temporarily hold the cycle length.
    
    	i = argv[0][1]; //Because I know where input will be I wanted to get it directly from command line
    	j = argv[0][2];
    
    	if( argc > 3)
    	{
    		printf("too many arguments\n");
    		return 0;
    	}
    
    	for(count = 0, temp = 0; i <= j; i++)
    	{
    		while(i != 1)
    		{
    			if(i % 2)//if i is odd
    			{
    				i = (3 * i)+1;
    				temp++;
    			}
    		
    			else // assumes that i is even
    				i = i/2;
    			temp++;
    		
    		}
    		
    		if(temp > count) //stores the largest temp number into count to be printed later
    		{
    			count = temp;
    			temp = 0;
    		}
    	
    		printf("%d %d %d\n", origi, j, count);
    		i++;
    	}
    	
    	return 0;
    }
    Can see where I messed up?
    Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    argv[0][1] is the second letter of the name of your program, and argv[0][2] is the third. The first argument lives (as a string, not an integer) in argv[1], and the second in argv[2].

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Ok, I need argv[1] and argv[2], then, and to convert those to an integer.
    Thank you. I'll see what happens.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    I've run the code through a debugger and made the above changes (thanks again, my head was about to explode). The guts of the code work, but it never stops the loop when it's equal to one. When I run the code the inner while loop executes but the conditions of the outer loop never seem to be met and the count variable never stops printing.

    This feels like it should be easy to spot the problem. I think I've been looking at the code too long.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	int origi;//hold the original value of *i
    	int i, j; 
    	int temp, count = 0;//"permanent" storage for cycle length
    			//temporary storage for the cycle length.
    
    	origi = i = atoi(argv[1]); //Because I know where input will be I wanted to get it directly from command line
    	j = atoi(argv[2]);//converts characters to their numeric equivalent
    
    	if(argc > 3)
    	{
    		printf("too many arguments\n");
    		return 0;
    	}
    
    	while(i < j)
    	{
    		while(i != 1)
    		{
    			if(i % 2)//if i is odd
    			{
    				i = (3 * i)+1;
    				temp++;
    			}
    		
    			else // assumes that i is even
    			{
    				i = i/2;
    				temp++;
    			}
    				
    			if(temp > count) //stores the largest temp number into count to be printed later
    			{
    				count = temp;
    				temp = 0;	
    			}
    		}
    		printf("%d %d %d\n", origi, j, count);
    		i++; 
    	}
    	
    	return 0;
    }
    Thanks again for all the help.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are printing origi, which indeed never changes. When you hit i++, i must needs equal one, which means it becomes two. So your while loop never stops, unless j is less than two.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Ooooooh. I get it!
    Man I feel stupid, I should have seen that!
    Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program segfaults without dumping a core
    By ladar in forum C Programming
    Replies: 4
    Last Post: 04-04-2005, 11:21 AM
  2. Special Allegro Information
    By TechWins in forum Game Programming
    Replies: 12
    Last Post: 08-20-2002, 11:35 PM
  3. Replies: 6
    Last Post: 07-10-2002, 07:45 PM
  4. Replies: 1
    Last Post: 12-26-2001, 03:00 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM