Thread: Still more problems with argv and argc

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

    Still more problems with argv and argc

    I know I've posted this code a couple of times now so I feel really stupid asking again for help but the requirements for the challenge that I'm working on changed a little bit on me.

    What I now need to do is take in a list of numbers, assuming it will be an even numbered list of numbers, and compute the cycle length of the pairs of numbers given. But when I accept the numbers at the command line, the program crashes. I know it's because of my inexperience with pointers and arrays of pointers.
    Here is the link to the challenge that I'm working on.
    Code:
    /*
    program overflows @ command line
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    
    	int *inci;	//holds the incremental value of i
    	int *origi;	//hold the original value of i to increment
    	int *num1, *num2; //numbers to be compared
    	int count = 0;	//count is the more permanent storage for counting cycle length
    	int temp = 0;	//temp is the temporary storage for testing greatest cycle length
    
     
    	while(argc > 1)
    	{	
    		int i = 1;	
    		*origi = atoi(argv[i]);
    		*inci = atoi(argv[i]);
    		*num1 = atoi(argv[i]);
    		*num2 = atoi(argv[i+1]);
    			
    		while(*num1 <= *num2)
    			{
    				while(*num1 != 1)
    				{
    					if(*num1 % 2)//if i is odd
    					{
    						*num1 = (3 * (*num1))+1;
    						++temp;
    					}
    				
    					else // assumes that i is even
    					{
    						*num1 = (*num1)/2;
    						++temp;
    					}		
    				
    					if(temp > count) //stores the largest temp number into count to be printed later
    					{
    						count = temp;
    						temp = 0;	
    					}
    				}
    				*num1 = ++(*inci);
    				argc -= 2;
    				argv += 2;
    			}
    		printf("%d %d %d \n", *origi, *num2, count);
    	}
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You may be the first person to ever try to solve a number-based problem without using any numbers. origi, inci, num1, and num2 could point to integers, if you had any; since you don't, they can't.

    Presumably you just want ints, and not pointers to (nonexistent) ints.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Hehe, well I was always curious about how the wheel worked.
    I thought I was taking integers from the command line.
    Am I not?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, sort of. atoi will convert a string to a number, true; however num1 and num2 are pointers, not integers, and they currently point to New Jersey. And unfortunately for all of us, New Jersey is not an int, so your attempt to write an integer into New Jersey will fail miserably. num1 and num2 must be numbers (as their name suggests), not pointers to numbers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Why use pointers when all of the program can be done by just using ints.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if argc == 2
    argv[2] will be NULL

    so atoi(argv[2] ) will crash
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Using pointers without allocating them
    So why do you think you need to use pointers in the code?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Elysia View Post
    So why do you think you need to use pointers in the code?
    I thought I needed to use pointers because I was using argv.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So is there any special reason why argv is so special it needs to use pointers, then?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    Quote Originally Posted by Elysia View Post
    So is there any special reason why argv is so special it needs to use pointers, then?
    I thought that because it is an array of pointers it would be easier to use pointers to use it.
    Or I'm just so anxious to write really sophisticated programs I blindly decided to use pointers where they where unnecessary.
    Or both.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I see. It is certainly possible to use pointers to it, but you got the concept wrong.
    Pointers store memory addresses, so you would assign to the actual pointer, and not the pointee.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    So the way I did it, i was playing with the address not the value, correct?

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Just remove every single asterisk in that piece of code. Except this one:
    Code:
    int main(int argc, char *argv[])
    You didn't need to add any pointers of your own.
    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

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MikeyIckey View Post
    So the way I did it, i was playing with the address not the value, correct?
    You were trying to assign to a pointer to which you hadn't set a valid address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MikeyIckey View Post
    So the way I did it, i was playing with the address not the value, correct?
    the pointer has to first point to some location before you can dereference it ie access the value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  5. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM