UNKNOWN! runtime error

Discussion in 'C' started by lionaneesh, Mar 23, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Code:
    #include<stdio.h>
    
    void reverse(char str[]);
    
    int main()
    {
        char str[100];
        int i;
    
        printf("Enter to reverse : ");
        scanf("%s",str);
        reverse(str);
        return(0);
    }
    /* Function */
    
    void reverse(char str[])
    {
    
        int i;
        for(i=99;i>=0;i--)
        {
            printf("%c",str[i]);
        }
        putchar('\n');
    }
    
    
    
    :embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse:embarasse\


    Why there is a runtime error in the above programme..........

    help!!!!!!
    URJENT!!!!!!!!
     
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Code:
    #include<stdio.h>
    [COLOR=Red]#include <string.h>[/COLOR]
    
    void reverse(char str[]);
    
    int main()
    {
        char str[100];
        int i;
    
        printf("Enter to reverse : ");
        scanf("%s",str);
        reverse(str);
        return(0);
    }
    /* Function */
    
    void reverse(char str[])
    {
    
        int i;
        [COLOR=Red]int z=strlen(str);[/COLOR]
        for(i=[COLOR=Red]z-1[/COLOR];i>=0;i--)
        {
            printf("%c",str[i]);
        }
        putchar('\n');
    }
    
    
    the error is that you reserve 100 positions in memory for the string entered
    but it uses actually only those the user enter.

    so if i enter the word car-->'c' 'a' 'r' '\0' so it uses 4 positions instead of 100
    so instead of starting from the end of 100positions you must start from the end of 4positions down to 0
     
    shabbir likes this.
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    That doesn't explain why there would be a runtime error. It would explain why garbage is displayed and if I modify the program to add
    Code:
    	for (i=0; i<100; i++)
    		str[i]='~';
    
    to main just after the int i declaration, the output is:
    Enter to reverse : hello
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ olleh

    but even without this modification I don't get a runtime error (in Visual Studio 2008 building the program in Debug mode).

    When it prompted you to enter a string, did you enter more than 100 characters? That might cause a runtime error - this is the infamous "buffer overflow" bug.

    If not then I can only suggest this is a compiler bug. Maybe the RTL has a problem with %c for non-printable characters. You can test this hypothesis with a simple program:
    Code:
    printf("Starting test\n");
    fflush(stdout);
    for (i=0; i<256; i++)
    {
      printf("%d %c ",i,i);
      fflush(stdout);
    }
    
    The fflush ensures that the last character before the runtime error is displayed; normally output is buffered which can cause confusion when a program crashes - it looks like it crashed before it actually went tits up.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice