Debug assertion failed error

This is a discussion on Debug assertion failed error within the C Programming forums, part of the General Programming Boards category; i m using visual studio 2012 and i pass three command line arguments as 10 20 30 and when i ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    14

    Debug assertion failed error

    i m using visual studio 2012 and i pass three command line arguments as 10 20 30 and when i m compile the program get error.....need help
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main(int n,char **p)
    {
    	int sum=0,i;
    	if(n>=2)
    	for(i=0;i<=n;i++)
    		sum=sum+atoi(p[i]);
    	printf("Sum=%d\n",sum);
        getch();
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,670
    The reason is probably that p[n] will be NULL (so says the standard) so atoi(p[n]) gives undefined behaviour.

    Additionally

    1) it is a bad idea to leave the return type (int) off main().
    2) Neither <conio.h> and getch() are standard in C.
    3) p[0] will (usually) be a string representing the name of the program when run. atoi(p[0]) is valid (as atoi() returns an error if it can't convert its argument to an int) but is probably not a good idea (say, if the first character of the program name contains a digit).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One more Debug Assertion Failed! Error
    By ilerleartik in forum C Programming
    Replies: 8
    Last Post: 03-17-2012, 05:49 PM
  2. debug assertion failed
    By khoavo123 in forum C Programming
    Replies: 1
    Last Post: 04-15-2011, 09:21 AM
  3. Debug Assertion Failed error
    By frankchester in forum C Programming
    Replies: 7
    Last Post: 12-02-2010, 09:55 AM
  4. debug assertion failed
    By rahulsk1947 in forum C++ Programming
    Replies: 7
    Last Post: 06-14-2009, 04:36 PM
  5. Debug assertion failed
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 10-17-2002, 05:34 PM

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