I discovered that the strcpy function simply copied one string to anther. For instance, if a program included the following statements:
char buffer[10];
----------
strcpy(buffer, "Dante");
the string "Dante" would be placed in the array buffer[]. The string would include the terminating null(\0), which means that six characters in all would be copied. I'm just wondering why we can't achieve the same effect more simply by saying?:
buffer = "Dante";
If I'm not mistaken, C treats strings far more like arrays than BASIC does.
Because strings aren't a data type in C. "Strings" are char*
s, so when you try to assign them, you are simply copying the memory address and not the characters into the buffer.
Consider this:
char* buffer;
buffer = malloc(20);
buffer = "Dante";
Why should it magically place "Dante" into the buffer?