Asignando a la matriz de caracteres un valor en C

Cuál es la diferencia entre:

char fast_car[15]="Bugatti";

y

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.

preguntado el 28 de agosto de 12 a las 14:08

You can use a string function like snprintf or strncpy to fill the array with a new value -

No use strncpy unless you're really really really sure that's what you want. Make sure the buffer is big enough and use strcpy en lugar de. -

@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 aquí. -

@harald: strncpy definitely is not peor que strcpy safety-wise. Of course you have to be sure about what you do in both cases. -

@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. -

2 Respuestas

El primero es un inicialización while the second is an asignación. 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.

Respondido 28 ago 12, 14:08

str_replace("modifiable", "mutable"); ;) - nick brunt

@Nick: String substitution is actually not that simple in C ;) - Niklas B.

@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

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 Pospischil

@hardpenguin You can use strcpy, use pointers etc. Depends on the nature of the data and the processing being done. - cnicutar

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.

Respondido 20 Feb 15, 20:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.