¿Cómo puedo restablecer una variedad de cuerdas en el lenguaje C?
Pregunta
Tengo un bucle que poca "Char Array_of_strings [100] [100];" En algún momento quiero poder limpiarlo de todas las cuerdas agregadas hasta ahora y comenzar a agregar desde la posición 0. ¿Cómo puedo limpiarlo/descansar en C?
Gracias
Solución
Suponiendo que de hecho está usando la matriz como cadenas, entonces algo así debería funcionar:
int i;
for (i=0; i<100; i++)
array_of_strings[i][0] = 0;
Por supuesto, no está tratando sus datos como cadenas, entonces es posible que deba ver algo como Memset.
Otros consejos
Puedes usar el función de memset Para configurar todos los caracteres a cero.
PD que el uso de una matriz bidimensional es bastante poco convencional para tratar con cuerdas. Intente avanzar hacia la asignación dinámica de cadenas individuales.
That data structure really has no inherent structure. You basically reset it by pretending there's nothing in it.
You have some counter that tells you which of the strings you've populated, right? Set it to zero. Done.
memset(array_of_strings, 0, sizeof(array_of_strings));
This is cleaner than putting a magic number for the size in there, and less likely to break down the road when someone changes the size of your strings array. Since you know the size of the array at compile time using sizeof will work.
You can also use bzero, which is a lot like memset, but only for zeroing. On some platforms bzero may be faster than memset, but honestly both functions are so fast, splitting hairs here is silly.
bzero(array_of_strings, sizeof(array_of_strings));
bzero requires you to #include memset needs #include