9

What is the difference between:

char fast_car[15]="Bugatti";

and

char fast_car[15];
fast_car="Bugatti";

Because the second one results with compile error:

error: incompatible types when assigning to type ‘char[15]’ from type ‘char *’

While the first one works fine. Putting a string in array in different place than array initialisation would be helpful.

hardpenguin
  • 159
  • 1
  • 2
  • 9
  • 2
    You can use a string function like `snprintf` or `strncpy` to fill the array with a new value – Niklas B. Aug 28 '12 at 13:25
  • 3
    Don't use `strncpy` unless you're really really really sure that's what you want. Make sure the buffer is big enough and use `strcpy` instead. – harald Aug 28 '12 at 13:29
  • @harald why is using strncpy a bad idea? it usually prevents the overflow. – hardpenguin Aug 28 '12 at 13:32
  • 1
    @hardpenguin, it silently truncates the string. That's very rarely what you want. In addition when truncating it does not terminate the string. See for instance [here](http://www.inspirel.com/articles/Strncpy_And_Safety.html). – harald Aug 28 '12 at 13:37
  • 2
    @harald: `strncpy` definitely is not *worse* than `strcpy` safety-wise. Of course you have to be sure about what you do in both cases. – Niklas B. Aug 28 '12 at 13:40
  • @NiklasB. If used as a blind replacement for `strcpy` it is. Used properly both functions are safe. – harald Aug 28 '12 at 13:47
  • 1
    @harald: In many cases, a truncated string is better than a buffer overflow (provided that you properly set the terminating null byte). How many security-critical bugs do you know that are the result of a truncated string? Of course it's even better to not introduce bugs at all, but we all know that this is not going to happen. The real solution is using a better string library than the C stdlib. – Niklas B. Aug 28 '12 at 13:58
  • Harald is right, strncpy is a very dangerous function because programmers don't know how it works, but they are brainwashed by other programmers on SO to use it, who don't know how to use it either. The pitfall is that strncpy expects the buffer size of the input string and _not_ the string length. If you pass the string length to it, the program will crash and burn. Just because strcpy might cause buffer overruns, that does not automatically make strncpy a safe function! – Lundin Aug 28 '12 at 14:14
  • Furthermore, strncpy is completely redundant as memcpy does the same thing, only much faster. Simply add the null termination manually after using memcpy. – Lundin Aug 28 '12 at 14:15
  • 1
    @Lundin: I think your second point about memcpy is very valid because we have to add the null byte manually in any case. – Niklas B. Aug 28 '12 at 14:34
  • possible duplicate of [Assigning value to char array](http://stackoverflow.com/questions/14915924/assigning-value-to-char-array) – Hesham Eraqi Jun 01 '14 at 08:35

2 Answers2

9

The first is an initialization while the second is an assignment. Since arrays aren't modifiable values in C you can't assign new values to them.

Mind you, you can modify array contents, you just can't say fast_car = .... So the contents are modifiable, the arrays themselves are not.


Using the same symbol = for these widely different concepts is of debatable value.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 2
    `str_replace("modifiable", "mutable");` ;) – Nick Brunt Aug 28 '12 at 13:25
  • 2
    @Nick: String substitution is actually not that simple in C ;) – Niklas B. Aug 28 '12 at 13:26
  • @cnicutar , can you suggest another solution? It is about manually assigning few array variables from the structure. Using pointer to char works, but isn't it a bad solution for managing memory? – hardpenguin Aug 28 '12 at 13:28
  • 1
    Additionally, a reason that initialization can set an array to a whole string and assignment cannot is that initialization is accomplished by storing the data executable image, where it “naturally” forms a part of the program’s memory when loaded. It does not cause any run-time operations beyond loading the program. In contrast, assignment requires copy operations, and this is a more involved process than C was originally designed for. – Eric Postpischil Aug 28 '12 at 13:29
  • 1
    @hardpenguin You can use `strcpy`, use pointers etc. Depends on the nature of the data and the processing being done. – cnicutar Aug 28 '12 at 13:30
  • Is there a reason why arrays behave that way? The only reason that I can think of is this: the size of the function frame is calculated at compile time and changing the array size afterwards also means changing the space requirement for that function call. And if you override this size later at runtime, you may potentially corrupt some other memory above/below in the stack since it would require to grow to fit that new size. – stdout Dec 26 '17 at 13:40
4
char fast_car[15]="Bugatti";

It says fast_car is an array and be initialized with string the "Buratti". Correct Usage :

char fast_car[15];
fast_car="Bugatti";

The first line is a declaration of char array(not initialized). Second, fast_car here is just an address(a pointer) of the first element in this array of char. The assignment of pointer fast_car to array of char "Buratti" is incorrect by difference type of value.

Utsav T
  • 1,515
  • 2
  • 24
  • 42
nofomopls
  • 343
  • 4
  • 17