0

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.

Aaron
  • 1,969
  • 6
  • 27
  • 47
  • 5
    please, Please, PLEASE stop using Turbo C. Seriously. There are compilers which actually conform to the standard, can produce 32 bit native executables, and most importantly are **free** -- e.g. MinGW or MSVC++ on Windows, or LLVM or GCC on Unix. – Billy ONeal May 27 '11 at 03:48
  • 1
    @Billy ONeal I think you're right. But I'm sorry, it's our school requirement... I have to do this. – Aaron May 27 '11 at 03:51

6 Answers6

9

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?

Marlon
  • 19,924
  • 12
  • 70
  • 101
3

Because an "array" in C is a chunk of memory. There's no pointer to assign to.


If you're asking why the syntax isn't like that: Well, what would happen if the lengths were different?

user541686
  • 205,094
  • 128
  • 528
  • 886
3

Address of array is not changeable. In other sense you can consider,

char buffer[20];

is a compile time equivalent of,

char* const buffer = (char*)malloc(20);

Now, since buffer address cannot be changed, one cannot perform operations like:

buffer = "Dante"; // error 'buffer' address is not modifiable
iammilind
  • 68,093
  • 33
  • 169
  • 336
2

When you write buffer, it is treated as a pointer to the first element of the array. Of course, *buffer or buffer[0] is the first element. Since buffer is just a pointer, you can't assign a whole bunch of data like "Dante" to it.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • Not true -- an array is a datatype in C. Do not confuse arrays with pointers -- they are not the same. Just because an array can decay into a pointer to its first element does not mean that the array *is* the pointer to the first element. e.g. `int myArray[42];` does **not** declare a pointer. – Billy ONeal May 27 '11 at 03:54
  • @Billy, I was expecting such a comment. I can't imagine array to be a data type. it's a collection of data. how it can simply be a type? oh, and yes, i know that the pointer and array aren't same. – Donotalo May 27 '11 at 03:56
  • 1
    Because the standard says so. And because it changes how `sizeof` works -- `sizeof(myArray)` (from above) is `42 * sizeof(int)` -- `sizeof(int *)` is always the size of a pointer. Arrays carry size and dimension information. Pointers do not. – Billy ONeal May 27 '11 at 03:57
  • I know that in C, a string is not a formal data type as it is in some languages. Instead, it is an array of type char. – Aaron May 27 '11 at 03:59
2

you can't do buffer = "Dante" because there is no "string" data type in C, only arrays.

Now you CAN however do...

char buffer[10] = "Dante";

but if the length of the string is unknown you can do...

char buffer[] = "Dante123456678";

but only during initialization, meaning you can't do...

char buffer[];

buffer = "Dante";
Heshan Perera
  • 4,592
  • 8
  • 44
  • 57
2
  • If char buffer[128]; was the declaration, then buffer refers the first location of the array, so buffer = "Dante" will try assigning the address of the string onto the address which is stored in the array. Memory address location in the array are read only and statically assigned when compiled. So you cannot do buffer = "Dante" as it attempts to change an address location which points to some other location fixed at compile time. These locations cannot be written.

  • If char *buffer; was the declaration, then buffer is a pointer to a char type variable which can point to a starring chunk of memory blocks. So when you do buffer = "Dante" the address of the string into buffer. When will show the string when you print it, as it points to a string starting address, which is compiled in and stored in the executable. But this is not a preferred method.

  • If you do char arr[] = "Dante"; the string "Dante" gets stored in the .text section where you can write, so arr[0] = 'K' something like this , ie modification is possible.

  • If you do char *arr = "Dante"; then the string "Dante" gets stored in the .rodata or similar location which is not writeable. This is because string literals are not modifiable as per the standards.

phoxis
  • 60,131
  • 14
  • 81
  • 117