strcat - strcpy

This is a discussion on strcat - strcpy within the C Programming forums, part of the General Programming Boards category; Why this code doesn't work? <code> char string[10]; strcat( string, "hello" ); puts ( string ); </code> and why when ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    strcat - strcpy

    Why this code doesn't work?
    <code>
    char string[10];

    strcat( string, "hello" );
    puts ( string );
    </code>

    and why when i write
    <code>
    char string[10] = "";

    strcat( string, "hello" );
    puts ( string );
    </code>
    it will work?

    Could you also tell me the difference between char string[10]; and char string[10] = ""; ?

    And why this problem won't appear when using strcpy() ????

    Mena meny thx - Polor

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    let me exaggerate, this is a string
    char string[10] = ""'
    it is because the string is this
    "\0", no '\0'
    this is not a string,
    char string[10];
    this is memory allocated for a string.

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156
    Originally posted by Salem
    > Could you also tell me the difference between char string[10]; and char string[10] = ""; ?
    One is initialised to contain a zero-length string, and the other contains junk which has no defined end ...
    which as Salem mentioned contains uninitialized data. IOW, whatever happens to be in memory when string[] is created is it's contents, otherwise know as junk.

    And by the way, it's [code] not <code>
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    ok, but...

    ok, but what about my other 2 questions?
    and what does zero-length string mean?

    Thx - Polor

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    12

    Thanks!

    Ok, thanks! I got it now!
    - Polor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv change
    By dracayr in forum C Programming
    Replies: 9
    Last Post: 04-10-2009, 02:49 AM
  2. problem in my strcpy strcat function
    By genie in forum C Programming
    Replies: 7
    Last Post: 11-07-2008, 08:36 AM
  3. Replies: 2
    Last Post: 06-03-2008, 12:57 AM
  4. strcpy() and strcat()
    By Moony in forum C Programming
    Replies: 5
    Last Post: 07-03-2006, 01:18 AM
  5. strcat or strcpy?
    By dirgni in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2002, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21