Thread: Problem with strcpy

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    5

    Problem with strcpy

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
    
    //Declaring Variables
    char buffer_one[1], buffer_two[1];
    
    strcpy(buffer_one,"1");
    strcpy(buffer_two,"1");
    
    printf("[BEFORE] buffer_two is at address %p, and is \'%s\'\n",buffer_two, buffer_two);
    printf("[BEFORE] buffer_one is at address %p and is \'%s\'\n",buffer_one, buffer_one);
    Can't seem to figure out why this doesn't work. These are my results:


    [BEFORE] buffer_two is at address 0x7fff955b636e, and is '1'
    [BEFORE] buffer_one is at address 0x7fff955b636f and is ''


    O and Please and Thank you for any explanation.
    O, just in case I'm using gcc

    Target: x86_64-linux-gnu
    gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You need to give your arrays enough space to contain the string AND the terminating \0 character at the end.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Plus, it doesn't do anything to the address of the variable. That's always going to be the same. It's what it contains that changes.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    So the reason that the buffer_one isn't taking 1 is because I'm not terminating the char array? I mean I only want one byte ie the character 1 in the char array.

    The size of the array should be enough to take on just '1' right?

    The problem is that
    strcpy(buffer_one,"1");
    isn't taking on the value of 1.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The size of the array is enough to hold '1', but that isn't what you asked it to hold. You asked it to hold "1", which is two char's: the 1 and the '\0' (end of string marker char).

    And that's a problem.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    5
    AHHHHHHH! Thanks. Big help. Still pretty new, testing bounds.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    It might give you a better insight into how strings and pointers to strings if you understand exactly why your original code did what it did. Buffer_one has the value 0x7fff955b636f and Buffer_two has the value 0x7fff955b636e, which are addresses in memory. Now let's look at what is actually in those addresses after setting buffer_one but before setting buffer_two:

    Code:
    Address:     636e   636f   6370
    Contents:     ?      '1'   '\0'
    We haven't touched 636e yet so we don't know what's there, but the next two addresses contain the code for symbol '1' and the end-of-string marker '\0' respectively. Now let's set buffer_two, which begins at 636e. So 636e will contain '1' and the next address, 636f, will contain '\0'- overwriting the '1' that was there before:

    Code:
    Address:     636e   636f   6370
    Contents:     '1'   '\0'   '\0'
    Now when you attempt to print buffer-two, it will work as expected because it begins with a '1' and ends with a '\0' just like it should. But attempting to print buffer_one, which starts at 636f, will return the empty string because it now begins with '\0'. Buffer_two has interfered with buffer_one because you had only allocated enough space for one character but attempted to store two and so the two strings have overlapped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM