-
"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? :confused:
-
Well if team[] is an array of pointers, it will work...
-
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!
-
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
-
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!