There is a dynamic data structure in the PrintTabl function. I would make it so that the value of 3 fields of my structure is written in one line (first name, last name and middle name in 3 different variables)

void PrintTabl(LEL* p) { int len = 40;// Π΄Π»ΠΈΠ½Π½Π° строки + 1 элСмСнт ΠΏΡ€ΠΈΠ·Π½Π°ΠΊΠ° ΠΊΠΎΠ½Ρ†Π° строки char *str = (char*)malloc(len*sizeof(char)); strcat(str,p->inform.sname); strcat(str," "); strcat(str,p->inform.fname); strcat(str," "); strcat(str,p->inform.tname); strcat(str," "); printf("%-39s",str); printf("%-16s",p->inform.pnumber); printf("%-31s",p->inform.address); printf("%-11s",p->inform.family); printf("%-8s",p->inform.sallary); printf("\n"); free(str); printf("\n"); } 

After launching the program, we fill our queue with data when calling the PrintTabl function (and more specifically when the printf line is working ("% - 39s", str); 3 garbage characters are displayed (if you can call them that)) and after free (ptr) still stay enter image description here

  • You missed the feature of the function strcat . It does 2 things: 1) Looks for the position of the end of the first line (i.e., the character 0 ) 2) Starting from the found position, it adds the second line. And after malloc your first line is not really a string yet, i.e. the symbol 0 in it can be completely absent, and strcat when searching for it, can go far beyond the allocated memory and eventually cause a memory corruption error when trying to add a second line to it. So, with three garbage symbols at the beginning of the line, you are still very lucky. - zed
  • man asprintf () is much more convenient than your essay. - 0andriy
  • @ 0andriy The function is certainly useful, but it is not included in the standard and, accordingly, is not available on all platforms. - zed
  • @zed it can be implemented quite easily using snprintf () and malloc () . And it will turn out all the same more beautiful. - 0andriy

1 answer 1

 char *str = (char*)malloc(len*sizeof(char)); 

At this moment in this place in memory is rubbish ...

 strcat(str,p->inform.sname); 

And you add to it.

Or, after allocating memory, initialize

 str[0] = 0; 

or replace the first strcat with strcpy .