Thread: program returns invalid value for strlen

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    program returns invalid value for strlen

    Hi,

    i have started writing a program for palindrome checking of characters.
    i have written the following program
    Code:
    #include <stdio.h>
    int check(char *data,int len);
    
    int main(int argc, char **argv)
    {
        char array[100];
        int len=0;
    
        printf("Enter the string less than 100 chars\n");
        scanf("%s",array);
        len = strlen(array);
        check(array,len);
        return 0;
    }
    
    int check(char *data, int len)
    {
        char array[100]; 
        int index=0;
        data += len;
        while(len--)
            array[index++] = *data--;
    
        printf("%s%d",array,len);
    return 1;
    
    }
    i have not completed the program. But i tried to run the program i found that the len variable is -1. How the len variable is -1 it should have been the length of the string. why am i getting -1? Can someone please help me.

    Thanks and regards,
    satya

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while(len--)
    What value of len causes this loop to exit?
    What value will len have, when it exits the loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    yeah i understood, it is copying the null character in the first byte and hence it is not printing any array. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DialogBox returns -1. GetLastError returns error 1813.
    By Benji Wiebe in forum Windows Programming
    Replies: 14
    Last Post: 09-26-2011, 10:21 AM
  2. strlen causing my program to freeze
    By Beowolf in forum C Programming
    Replies: 2
    Last Post: 09-11-2007, 04:09 PM
  3. SetFileTime returns invalid handle
    By leonv in forum Windows Programming
    Replies: 5
    Last Post: 01-21-2007, 06:57 PM
  4. read() returns invalid file descriptor ???
    By hoho in forum C Programming
    Replies: 4
    Last Post: 08-04-2006, 10:54 AM
  5. float returns invalid value!!
    By q6z4k in forum C Programming
    Replies: 6
    Last Post: 04-15-2006, 04:08 PM