Thread: strange bus error

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16

    strange bus error

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char * argv[]) { 
    int i, j, k;
    
    
    	if (*argv[1] == '-') {
    		if (*++argv[1] == 'l') {
    		
    				j = *++argv[1];
    				for (i = 2; i< j+2; i++) {
    					k = strlen(argv[i]);
    					printf("&#37;s  lenght %d \n",argv[i], k);
    				}
    		}
    		
    		else {
    			printf("try again: \n");
    		}
    	
    		
    
    	}
    
    return 0;
    }

    when I launch my program for example via commanfd myprogram -l2 Micahel Schumacher the output is:

    Michael lenght 7
    Schumacher lenght 10
    Bus error

    I just can't figure what is causing the bus error. My compiler shows that this line if (*argv[1] == '-') is causing the problem.

    Any ideas?
    Last edited by Tehy; 05-22-2007 at 08:42 AM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your for loop condition is probably way off, among other things.

    I wouldn't write such cryptic code conditions with argv, since it makes debugging difficult, as you are finding out.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    oh, ok I'm just learning how to use command line arguments

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Try something like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	int i;
    	
    	for(i=0;i<argc;i++)
    	{
    		printf("argv[&#37;d] (len=%d) = %s\n",i,strlen(argv[i]),argv[i]);
    	}
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    thanks, I will

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > j = *++argv[1];
    You probably want the integer value of argv[1] here:
    Code:
    				j = *++argv[1] - '0';

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Finland
    Posts
    16
    Thanks swoopy, but I still get the bus error and compiler says that the problem is here if (*argv[1] == '-')

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Tehy View Post
    Thanks swoopy, but I still get the bus error and compiler says that the problem is here if (*argv[1] == '-')
    Try printing out j to see if it is 2.
    Code:
    				j = *++argv[1] - '0';
    				printf("j: %d\n", j);

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Tehy View Post
    Thanks swoopy, but I still get the bus error and compiler says that the problem is here if (*argv[1] == '-')
    Are you checking the argc? or you just going out of the array bounds?
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM