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.

11
  • 2
    You can use a string function like snprintf or strncpy to fill the array with a new value Commented Aug 28, 2012 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. Commented Aug 28, 2012 at 13:29
  • 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. Commented Aug 28, 2012 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. Commented Aug 28, 2012 at 13:40
  • 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. Commented Aug 28, 2012 at 13:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

str_replace("modifiable", "mutable"); ;)
@Nick: String substitution is actually not that simple in C ;)
@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?
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.
@hardpenguin You can use strcpy, use pointers etc. Depends on the nature of the data and the processing being done.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.