Thread: Error in runtime

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    40

    Error in runtime

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
    int main(void)
    {
        char ar[20],*p;
        int a;
        gets(ar);
        if(a=atoi(ar))
            printf("%d",a);
        else
           {
                while(true)
                {
                     p=strchr(ar,'s');
                     if(*p=='\0')
                         break;
                }
                printf("why?");
           }
        return 0;
    }
    umm in that code i don't know why it produces a runtime error....i know that the problem
    is somewhere in the memory but could someone help me so that it would not produce
    a run-time error...thanks!!

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    if(*p=='\0')
    strchr returns a pointer to the char if it is found and NULL if it s not found.
    hence, you want to check for -

    Code:
    if(p == NULL)
    '\0' is the null terminating string of a char array and is different than a null pointer.
    Cprogramming.com FAQ > NULL, 0, \0 and nul?

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Also, you'll have to check for 's' too in case its found otherwise you'll enter an infinite loop.

    Code:
    if( *p == 's')
    {
     // ..do something
      break;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    thanks spidey and elysia!!! i've forgot the difference between the '\0' and the NULL pointer...

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    umm guys i think 0 and '\0' is the same...correct me if i am wrong
    here's the proof:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char ar[5]="1234";
        short b;
        ar[0]=0; 
        printf("%s\n",ar);  
        for(b=0;b<5;b++)
            ar[b]='s';
        printf("%s\n",ar);
        ar[0]='\0';
        printf("%s\n",ar);
        return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sick
    guys i think 0 and '\0' is the same
    They have the same value and the same type. However, the point is that you should not use '\0' when you want a null pointer constant, because the contextual cue is that it has something to do with characters. You should use NULL, which is an implementation defined null pointer constant, or 0 if you wish to avoid the use of a macro.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    40
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  3. Visual Studio and .Net Runtime Framework dependency
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-08-2007, 07:52 AM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. Runtime error! Please help
    By Garfield in forum C Programming
    Replies: 7
    Last Post: 10-10-2001, 06:56 PM