I'm trying to ask for a word to be entered and then create a new memory space to copy the word entered by the user there, but I don't know how to do it.
I have this:
#include <string.h> // strlen, strcpy, strcmp, strcat
#include <stdio.h> // sprintf, scanf, ungetc, stdin
int main() {
char palabra[100];
printf("Palabra: ");
scanf("%s", palabra); // guardamos la palabra en el arreglo
int tam = strlen(palabra); // me guarda el tamaño de la palabra ingresada
printf("Escribiste: %s\n", palabra); // muestra la palabra
printf("Tamaño arreglo: %d\n", tam); // muestra el tamaño
// creamos memoria dinamica, y "nueva" apunta a la primer celda del arreglo
char *nueva = new char[tam + 1];
// creamos un puntero auxiliar para recorrer a "nueva" dejando la posibilidad
de acceder al primer caracter luego con el puntero "nueva" que ha quedado
intacto.
char *aux = nueva;
// creamos un puntero para recorrer el arreglo de la palabra ingresada
char *p = palabra;
// mientras el caracter de la celda del arreglo de palabra sea distinto a el
final de linea
while (*p != '\n') {
// el contenido a donde apunta aux es modificado por el contenido de a donde
apunta p
*aux = *p;
aux++; // apuntamos a la siguiente celda del arreglo
p++;
}
printf("Nueva palabra: %s\n", nueva); // mostramos la copia
delete[] nueva; // liberamos memoria de la copia
// la memoria del arreglo palabra se libera al finalizar el main() automaticamente
return 0;
}
But it doesn't work like that.
In the comments I leave how my reasoning is when writing that code. And I make comments so that they correct me if I say something that shows that I am wrong in how I understand some things.
Cheers!
In your code you indicate that you loop through the character formation as long as the character in the cell is other than the end of line , but nowhere have you added the end of line (
\n
) to the string you are copying.You can fix it like this:
Other things to keep in mind.
You're tagging the question as c++
new
and that's correct because you use the and operatorsdelete
that are unique to C++, but it's the only thing you use from that language in your code.If you want to program in C++ using the C++ headers, you must use the C++-adapted versions, in your case
<cstring>
and<cstdio>
, read this thread to learn more about it.However, my advice is that if you are programming in C++, abandon C practices and headers altogether by using
std::cin
instead ofscanf
andstd::cout
instead ofprintf
; besides usingstd::string
. If you need to practice with pointers, your code might look like this:You can see the code working in Wandbox 三へ( へ՞ਊ ՞)へ ハッハッ.
The character at the end of the string is
0
, when you printnueva
at the end of the string you may get strange characters, this is because the console has not found the end of the string (0
) but has found line break (\n
) and will continue to show characters until a chain end is found, whether or not it belongs to the original chain.The problem is the while condition. You have to change '\n' to '\0'.