how to print char array in c++

39,483

Solution 1

You haven't initialized dest

char dest[1020] = ""; //should fix it

You were just lucky that it so happened that the 6th (random) value in dest was 0. If it was the 1000th character, your return value would be much longer. If it were greater than 1024 then you'd get undefined behavior.

Strings as char arrays must be delimited with 0. Otherwise there's no telling where they end. You could alternatively say that the string ends at its zeroth character by explicitly setting it to 0;

char dest[1020];
dest[0] = 0;

Or you could initialize your whole array with 0's

char dest[1024] = {};

And since your question is tagged C++ I cannot but note that in C++ we use std::strings which save you from a lot of headache. Operator + can be used to concatenate two std::strings

Solution 2

Don't use char[]. If you write:

std::string dest;
std::string source( "baby" )
//  ...
dest += source;

, you'll have no problems. (In fact, your problem is due to the fact that strcat requires a '\0' terminated string as its first argument, and you're giving it random data. Which is undefined behavior.)

Solution 3

your dest array isn't initialized. so strcat tries to append source to the end of dest wich is determined by a trailing '\0' character, but it's undefined where an uninitialized array might end... (if it does at all...)

so you end up printing more or less random characters until accidentially a '\0' character occurs...

Share:
39,483
Carlitos Overflow
Author by

Carlitos Overflow

Updated on November 23, 2020

Comments

  • Carlitos Overflow
    Carlitos Overflow over 3 years

    how can i print a char array such i initialize and then concatenate to another char array? Please see code below

    int main () {
    char dest[1020];
    char source[7]="baby";
    cout <<"source: " <<source <<endl;
    cout <<"return value: "<<strcat(dest, source) <<endl;
    cout << "pointer pass: "<<dest <<endl;
    return 0;
    }
    

    this is the output

    source: baby
    return value: v����baby
    pointer pass: v����baby
    

    basically i would like to see the output print

    source: baby
    return value: baby
    pointer pass: baby
    
  • James Kanze
    James Kanze over 12 years
    It's undefined behavior, period.
  • Carlitos Overflow
    Carlitos Overflow over 12 years
    what is the difference by initializing char dest[1020] = ""; or char dest[1020] = {0}; as pointed by linuxuser27 above
  • Tristram Gräbener
    Tristram Gräbener over 12 years
    You can use char[], but then you need to tag the question as C, not as C++ ;)
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    @James: Technically, yes, but practically, on all implementations I know of, if it so happened that one of the characters were zero, nothing worse than wrong results would happen. But in any case, I agree that it's UB anyway
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    @user945511: both char dest[1020] = {0} and char dest[1020] = {}; set ALL characters to 0. char dest[1020] = "" sets just the first.
  • Carlitos Overflow
    Carlitos Overflow over 12 years
    i cannot use std::string because im dealing with characters
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    @user945511: Huh? You can index a string with [] just like char[]
  • fefe
    fefe over 12 years
    @ArmenTsirunyan: char dest[1020]="" would also set all characters to 0 according to the new standard that just came out, though not clearly stated in the previous standard in 2003.
  • James Kanze
    James Kanze over 12 years
    @TristramGräbener You can use char[] in C++ too. It just makes your life a lot harder. I'd consider it something reserved for the experts, working in low-level close to the hardware code.
  • James Kanze
    James Kanze over 12 years
    @ArmenTsirunyan Accessing uninitialized memory is undefined behavior, precisely because you can't know what is in it (and something in it might cause problems). In this case, if the memory contains a '\0' at least five characters before the end, the effect of the undefined behavior will probably not crash the program, but that doesn't make it defined. (You're describing a typical behavior, in the face of undefined behavior.)
  • Tristram Gräbener
    Tristram Gräbener over 12 years
    @JamesKanze it was just a joke. Of course you can. And in some extremely weird situations it might even be better.
  • Armen Tsirunyan
    Armen Tsirunyan over 12 years
    Clearing memory with memset is more secure than initializing with = {}; ? oO