Thread: What's wrong?

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    What's wrong?

    Hi, what's wrong with this code? I just followed the requirements of using strcat.
    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char arr2[]="I love mom";
        char arr3[]="45";
        strcat(arr2,arr3);
        printf("%s", arr2);
        return 0;
    }
    Last edited by Romyo2; 08-13-2015 at 07:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    arr2 has no space for any characters to be concatenated to it, i.e., it fails the requirement that there be enough space for strcat to write to without going out of bounds.
    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 ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    > I just followed the requirements of using strcat.
    Perhaps you didn’t follow it to an extend where it explicitly specified this

    "Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string."
    I would also suggest that you use strncat. Rather than strcat which will help great deal in avoiding potential buffer overflow sequences
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. New to C What Is wrong with this...
    By headshot119 in forum C++ Programming
    Replies: 18
    Last Post: 08-08-2009, 05:02 AM
  3. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  4. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  5. Replies: 9
    Last Post: 07-15-2004, 03:30 PM