Thread: "strcpy" vs " = "

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    11

    Question "strcpy" vs " = "

    I inserted the following code into my program before I remembered what variable type I was using and it works:

    Code:
    void somefunction ()
    {
    
    char name[6][20];
    char drop[20];
    int total = 6;
    
    cin >>drop //variable user wants to remove from stack
    
    for (int j = 0; j < total; j++)
       if (strcmp (drop , name[j]) == 0)
       {    
            for (int k = j; k < total; k++)
            team[k] = name[k + 1];
    
       }
    
    }
    When I run this function and print my variables to screen, the "drop" has been removed and all the "name" variables have been moved down. Does anybody know why this works even though I know I'm supposed to have to use strcpy?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well if team[] is an array of pointers, it will work...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    When you use the word "pointer," I think of dynamic memory... linked lists... etc. I thought that was the only time you use pointers.

    If my assumption is correct, then that doesn't apply... unless I'm missing something. My "char team[][]" is a basic two-dimensional array. I'm not using dynamic memory in this program.

    If what I know about pointers isn't complete, please let me know.

    Thanks!

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    all right here i go, the reason strcpy is better than using the assignment operator(=) is that it does not just assign the address. when working with an array by its name its like working with a pointer, if you assign one array to another your just saying this is your adress now, if you have dynamic memory you now have two pointers to one adress, freing one destroys the other which can lead to major bugs, strcpy avoids this and thus is your friend

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    11
    Ok. I can understand that. I did it by accident and just wondered why it worked. I'll stick with "strcpy" to be better safe than sorry.

    Thanks you two!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using "STRCPY" to remove a word
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 04:37 PM