5

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]);
Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
Chris N
  • 125
  • 1
  • 7
  • 1
    the "equal sign" as you call it is more commonly called "assignment operator" (`=`). The "equals operator" would be `==`. I've taken the liberty to edit your question to avoid confusion. – Sander De Dycker Oct 10 '18 at 09:43
  • 4
    The second one is undefined behavior. You're writing the string to an address where there's just a pointer, without any memory allocated to hold that string. – Blaze Oct 10 '18 at 09:44

1 Answers1

12

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.

V. Kravchenko
  • 1,859
  • 10
  • 12
  • @bolov well, technically speaking, showing data on scheme as multi-character literal is okay, I think. Because value of expression `"s"` is pointer, and value of `'s'` is representation of `'s'` itself. But yeah, good point, it would be simpler to read for novice with double quotes. – V. Kravchenko Oct 10 '18 at 09:59
  • Thanks for the great explanation. – Chris N Oct 10 '18 at 10:03