0

So, I'm working on Binary Trees and I need to move information from one node to another one (structs). The thing is that I have this function that uses strcpy to copy the names and surnames from one node to other node (both sent as pointers) and when I try to run that part of the code in the program, it crashes.

Here is the function that copies the info (It has to copy the info from nodo2 to nodo1):

void MoverDatos(Tarbol *nodo1, Tarbol *nodo2)
{
    strcpy((*nodo1)->appat, (*nodo2)->appat);
    strcpy((*nodo1)->apmat, (*nodo2)->apmat);
    strcpy((*nodo1)->nombre, (*nodo2)->nombre);
    (*nodo1)->matr=(*nodo2)->matr;
}

As it wasn't working that way, I tried to use this one instead:

void MoverDatos(Tarbol *nodo1, Tarbol *nodo2)
{
    char cad1[20];
    char cad2[20];
    char cad3[20];

    strcpy(cad1, (*nodo2)->appat);
    strcpy(cad2, (*nodo2)->apmat);
    strcpy(cad3, (*nodo2)->nombre); 

    strcpy((*nodo1)->appat, cad1);
    strcpy((*nodo1)->apmat, cad2);
    strcpy((*nodo1)->nombre, cad3);
    (*nodo1)->matr=(*nodo2)->matr;
}

But it didn't work either. I don't know if I'm not setting the arguments the right way or if I need to use another function, so any help or sugestion would be great. Thanks in advance.

It does compile with those arguments. I'm not getting any error message, it just crash and says that "program.exe has stopped working".

This is the struct that I'm using:

typedef struct _tdato
{
    long matr;
    char nombre[20];
    char appat[20];
    char apmat[20];
    struct _tdato *sig;
    struct _tdato *ant;
    struct _tdato *padre=NULL;
}Tdato;

typedef Tdato *Tarbol;

The crash comes when I trie to copy a char[ ] to nodo1.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    Welcome to StackOverflow. Please read the guidelines on how to use the site and ask good questions. Specifically, you need to show a minimal but complete example. Even the code you have shown is incorrect (`(*nodo1)->appat` probably won't compile). Without seeing the definition of `Tarbol` it is impossible to help you. – Jonathan Wakely May 16 '16 at 18:25
  • What is exact error message when the program crashes? It is basic and very important information. – Adam May 16 '16 at 18:27
  • Please edit the question to add the necessary information, don't add it to a comment. – Jonathan Wakely May 16 '16 at 18:46
  • And read http://stackoverflow.com/help/on-topic especially this part: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – Jonathan Wakely May 16 '16 at 18:49
  • Please check your `main()`. Maybe you're trying to copy to/from a null or uninitialised `Tarbol` (which is a pointer itself) – trolley813 May 16 '16 at 19:00
  • Hi @hyst329, actually, the `Tarbol`(nodo1) to which I'm trying to copy the info is initialised as NULL, could that be a problem? – Oscar Corona May 16 '16 at 19:12
  • @OscarCorona Yes, it really is Because the members addresses are on a fixed offset from the struct address itself, you're trying to copy to location which is the offset itself (this is because NULL is zero on most platforms). Usually, it isn't the desired location and probably protected by OS, so you get a crash. To fix it you need to `malloc()` the memory for nodo1 – trolley813 May 16 '16 at 19:17
  • @hyst329 oooohhh I see. I'll try that. Thank you very much. – Oscar Corona May 16 '16 at 19:22
  • @OscarCorona Not at all. Good luck! – trolley813 May 16 '16 at 19:23
  • Strcpy copies src to dst including the terminating \0. If there is no \0, strcpy keeps on going and copies junk until it finds \0. If the output buffer is smaller than the input string, strcpy writes into whatever space exists after the buffer and can also produce a crash. – Marichyasana Aug 19 '16 at 07:00

0 Answers0