Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C - strcpy vs assignment [duplicate]

Tags:

c

Are they any difference if i use strcpy() fuction and the assignment operator ?

char word[][40],*first;

Below is the 2 example.

*first=word[0]; 
strcpy(first,&word[0]);
like image 659
Chris N Avatar asked Jan 02 '23 18:01

Chris N


1 Answers

strcpy performs deep copy. It copies data contained in memory at address, which is equal to value of pointer, to memory at address, which is equal to second pointer.

Assignment simply assigns second pointer value of the first pointer.

Here is a small figure for you:

A -> "some data           "
B -> "some other data     "

After assignment:

A -> "some data           "
   /
  /
B    "some other data     "

After strcpy:

A -> "some data           "
B -> "some data           "

Mind the fact that memory for strcpy to copy to must be allocated beforehand.

like image 137
V. Kravchenko Avatar answered Jan 11 '23 08:01

V. Kravchenko