Thread: program crashing despite no errors and warnings

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    16

    program crashing despite no errors and warnings

    my main aim here, is to get the command line arguments, and ideally assign them to variables and them and do something.. trouble is, the program doesnt seem to work..and inspite of 0 errors and warnings when building, when i run the first program, it prints out the number of arguments, but then crashes.

    Code:
    #include <stdio.h>
    
    void main (int argc, char *argv)
    {
        int n;
    
        printf("\n No. of arguments:%d",argc);
    
        printf("%s",argv[1]);
    
        for(n=0;n<argc;n++)
        {
            printf("\n Argv(%d) is %s",n,argv[n]);
        }
    }
    and this is the output from the problem details,
    Problem signature:
    Problem Event Name: APPCRASH
    Application Name: user_interface.exe
    Application Version: 0.0.0.0
    Application Timestamp: 4c869bfd
    Fault Module Name: msvcrt.dll
    Fault Module Version: 7.0.7600.16385
    Fault Module Timestamp: 4a5bda6f
    Exception Code: c0000005
    Exception Offset: 0000d193
    OS Version: 6.1.7600.2.0.0.256.48
    Locale ID: 3081
    Additional Information 1: 0a9e
    Additional Information 2: 0a9e372d3b4ad19135b953a78882e789
    Additional Information 3: 0a9e
    Additional Information 4: 0a9e372d3b4ad19135b953a78882e789


    have i forgotten anything?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    there are three ways using argc, argv

    Code:
    int main(int argc, char **argv);
    Code:
    int main(int argc, char *argv[]);
    Code:
    int main(int argc, char argv[][]);
    Last edited by brack; 09-07-2010 at 03:00 PM.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try this:
    Code:
    int main (int argc, char *argv[])
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    16
    ok so
    Code:
    void main (int argc, char *argv[])
    did the trick. but now my other problem is taking the arguments, and comparing it to other strings and doing something when they're equal. have i done correct use of pointers? argv is a pointer isnt it?

    Code:
    void main (int argc, char** argv[])
    {
        int n;
        char *inputVar;
        char *initInput;
        char *turnInput;
        char *simInput;
    
        //resolveCommandLineInput(argc,argv);
    
        printf("\n %s",argv[1]);
    
        initInput = "-i";
        turnInput = "-t";
        simInput  = "-s";
    
        if (argc > 2)
        {
            helpDisplay();
        }
    
        inputVar = argv[0];
    
        printf("\n %s", inputString);
    
    
        printf("\n got to begining of if statements");
    
        printf("\n %s %s %s %s",&inputVar,&initInput,&turnInput,&simInput);
    
        if(strcmp(inputVar,initInput) == 0)
        {
            printf("\n init input given");
        }
        else if(strcmp(inputVar,turnInput) == 0)
        {
            printf("\n turn input given");
        }
        else if(strcmp(inputVar,simInput) == 0)
        {
            printf("\n sim input given");
        }
        else
        {
            printf("\n Got here");
        }
    
        printf("\n got to end of the if statements");

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    16
    ohh..and in the print statement where i print out the strings which need to be compared to and the input argument, when i run this program, it jus gives out weird symbols..not what has been input..

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by yohanevindra View Post
    ohh..and in the print statement where i print out the strings which need to be compared to and the input argument, when i run this program, it jus gives out weird symbols..not what has been input..
    Code:
    printf("\n %s %s %s %s",&inputVar,&initInput,&turnInput,&simInput);
    Lose the & in front of each string variable in your printf statement. Also, main returns an int - so don't make the return type void. Furthermore, because main returns an int, you should put a return statement at the end of the main function (unless you are trying to conform to C99, where you don't necessarily need to put an explicit return statement there).

    Code:
    void main (int argc, char** argv[])
    Your argument vector is not declared properly. As others have indicated, you should do it something like this:

    Code:
    int main(int argc, char *argv[])
    Edit: What sort of warnings have you got turned on for your compiler? It should have complained about some of the things you are doing with your code.
    Last edited by kermit; 09-07-2010 at 03:47 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    [edit] kermit has beaten me to the post. Sorry about duplication. [/edit]

    Why not to use void main(): SourceForge.net: Void main - cpwiki

    Standard practice is to print newlines after a line of text, not before. This can be important when you're using print statements for debugging purposes, since the console may not flush (display some text) until a newline is printed, making you think the debugging message wasn't reached when in fact it was.

    If a variable is declared as char *inputvar; then it is a pointer to an array of characters, that is, a string. So if it was pointing to a valid string, you could use something like
    Code:
    printf("%s\n", inputvar);
    But as soon as you go &inputvar, you get a pointer to a string, which will crash printf() quite handily.

    Also, argv[0] is the name of the program, not the first parameter passed. Use argv[1], after making sure it exists by ensuing that argc >= 2.

    Last note: as you may be aware, using char* pointers as strings is fine as long as the data you're pointing to exists. For example, you can say
    Code:
    char *flag = "-t";
    because "-t" is a string literal and is stored in static memory. You can also assign a char* pointer to some data from argv[], because the program which executed your program made space for and is storing the argv[] array. But you cannot read data from the user into a char* pointer unless you make the pointer point at a memory region you can fill in. You could use e.g.
    Code:
    char *data = malloc(10);
    fgets(data, 10, stdin);
    free(data);
    or
    Code:
    char data[10];
    fgets(data, sizeof data, stdin);
    if you wanted to get user input, for example.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    16
    thanks kermit and dwks!!!got it working!!!i really need to get upto date on my pointers..i've been learning C for ages and its still hard to get my head around it..know of any good tutorials online?

    Quote Originally Posted by kermit View Post
    Edit: What sort of warnings have you got turned on for your compiler? It should have complained about some of the things you are doing with your code.
    i'm using codeblocks..is that a good compiler? In the compiler/debugger settings there are heaps of warnings which i have turned off...should i turn them on?i've attached a screen shot. I remember in visual studio you could define the warning level and i remember that we always used warning level 3 i think.

    Quote Originally Posted by dwks View Post
    Last note: as you may be aware, using char* pointers as strings is fine as long as the data you're pointing to exists. For example, you can say
    Code:
    char *flag = "-t";
    because "-t" is a string literal and is stored in static memory. You can also assign a char* pointer to some data from argv[], because the program which executed your program made space for and is storing the argv[] array. But you cannot read data from the user into a char* pointer unless you make the pointer point at a memory region you can fill in. You could use e.g.
    Code:
    char *data = malloc(10);
    fgets(data, 10, stdin);
    free(data);
    or
    Code:
    char data[10];
    fgets(data, sizeof data, stdin);
    if you wanted to get user input, for example.
    and thanks heaps for this dwks!this got me understanding of how pointers and strings kinda works..need to find a good tute so i can understand pointers properly..

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    For online tutorials about pointers, have a look at this and this.

    The material for the second link is done by someone who used to be a regular here on this board. In my opinion, she is a first rate programmer who really knows her stuff. You won't go wrong on either link though, so give them both a good going over.

    For the compiler (which is gcc, by the way, and is an excellent compiler), you will want to turn on -Wall and -Wextra. The -Wall switch is a little bit deceiving, as it does not really turn on "all" warnings (though it turns on quite a few. You can read about which ones in the man page linked below). The -Wextra switch turns on quite a few more warning switches that -Wall doesn't get (again, see the manual page for details). Between these two, you will be covering an awful lot of the important things, especially for someone just starting out. Pay attention to the warnings they produce, and you will find yourself learning a bit too. You may as well give the gcc man page a read. It is a lot to look at, and you certainly don't need to know it all, but give it a skim, to gain a general idea of what is available to you, and then slowly read it bit by bit to know it better over time.
    Last edited by kermit; 09-07-2010 at 06:48 PM.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    16
    thanks heaps for that kermit!!!

    will have a look at those 2 links and also just enabled those 2 extra warnings..hopefully i'll get my program working soon

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Right, I'm pretty sure you understand this, but pay attention to those warnings, Don't go disabling them or ignoring them. Fix them.
    If you don't understand them, ask. Ignoring them is a sure way to mess up your program, and fixing them goes a long way towards fixing a lot of bugs.
    Oh and Code::Blocks is an IDE, not a compiler. GCC is usually the compiler used by it, though it can use more.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-06-2008, 04:06 AM
  2. Errors when building program
    By tay_highfield in forum C Programming
    Replies: 2
    Last Post: 03-07-2003, 11:08 AM
  3. Some errors and warnings i dont understand
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 10-18-2002, 11:16 AM
  4. Average Rainfall Program Errors
    By JamesAnthony23 in forum C Programming
    Replies: 1
    Last Post: 09-11-2002, 10:44 PM
  5. Simple Program wont execute after compiles w. no errors
    By Unregistered in forum Windows Programming
    Replies: 1
    Last Post: 02-03-2002, 04:24 PM