Thread: Why strcat is not working in this case?

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Why strcat is not working in this case?

    This is throwing a runtime error
    Code:
    #include<studio.h>
    #include<string.h>
    
    int main()
    {
    const char *x="hello";
    char *s=" world";
    strcat(s,x);
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because attempting to modify a string literal results in undefined behaviour, and you're doing just that by attempting to concatenate "hello" with the string literal " world". In fact, I told you in another thread:
    Quote Originally Posted by laserlight
    c should have been declared as a pointer to const char instead so as to avoid accidentally attempting to modify a string literal, which would result in undefined behaviour.
    So why did you persist in declaring s to be a pointer to non-const char? You should have declared it to be a pointer to const char, and then your mistake would have become obvious.
    Last edited by laserlight; 11-02-2016 at 01:36 AM.
    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

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    Now even on changing the code tho this, once it had printed " worldhello" but rest of the times it said runtime error.

    Code:
    #include<studio.h>
    #include<string.h>
    
    int main()
    {
    const char x[]="hello";
    char s[]=" world";
    strcat(s,x);
    printed("%s",s);
    return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    As well as making sure you're trying to modify writeable memory, you also need to ensure you have enough memory to write to.

    Code:
    const char x[]="hello";
    char s[]=" world";
    strcat(s,x);
    The compiler will measure the length of your string constants, and allocate just enough memory for s and x.

    If you're trying to see "hello world", then your strcat parameters are the wrong way round.
    Code:
    char x[30]="hello";  // 30 chars, initialised to "hello\0\0\0\0<20 more \0>\0" 
    char s[]=" world";
    strcat(x,s); // append s onto the end of x
    Since "studio.h" and "printed" don't exist, how are you compiling this?
    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.

  5. #5
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    Code:
    char x[6]="hello";  
    char s[]=" world";
    strcat(x,s);
    Despite not giving enough space to x this code is giving output as "hello world". Why?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Despite not giving enough space to x this code is giving output as "hello world". Why?
    Because of pure dumb luck.

    I would suggest you learn how to do it properly, instead of experimenting with all sorts of undefined behaviour and wondering why various things do (or do not) happen.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Case Statement not working
    By cmajor28 in forum C++ Programming
    Replies: 3
    Last Post: 03-17-2015, 03:16 PM
  2. switch-case statement not working properly
    By Shinzu911 in forum C Programming
    Replies: 11
    Last Post: 04-25-2012, 10:41 AM
  3. C: Default case in switch not working
    By LunaLux in forum C Programming
    Replies: 5
    Last Post: 04-24-2011, 08:46 AM
  4. strcat not working
    By Bargi in forum C Programming
    Replies: 7
    Last Post: 02-22-2009, 11:08 AM
  5. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM

Tags for this Thread