There is a structure - discipliny with a variety of fields and an array of disciplina[n] . Dynamically allocating memory in this form

 disciplina = (discipliny*)malloc(sizeof(*disciplina))*n; 

It is necessary that when reading from the file of new elements the memory for the array is automatically expanded. I tried to write in different variations to write

 disciplina = realloc(disciplina, sizeof(*disciplina)*i); 

but all the errors are written, help write this function correctly.

Full structure code:

struct discipliny {char nazvanie[20], otchet[6]; int vse_chasy; }

Announcement in Maine:

 discipliny *disciplina; disciplina = (discipliny*)malloc(sizeof(disciplina)*n); 

File reading function:

while(fscanf(file, "%d\n")==NULL) {

  fscanf(file,"%s", disciplina[i].nazvanie); fscanf(file," %d\n", &(disciplina[i].vse_chasy)); i++; disciplina = realloc(disciplina, sizeof(disciplina)*i); *n=i; } fclose(file); 
  • 3
    Which mistakes? It looks like you have an initial distribution with an error: (discipliny *) malloc (sizeof ( disciplina) * n) instead of (discipliny ) malloc (sizeof (disciplina) * n) should be punished. You are too lazy to write (copy / paste), what kind of errors. What else, if not disrespect towards the readers? Your question can be considered such behavior? - alexlz
  • I just didn’t want to load the code much with the user and find out how to write this function correctly ... In the initial form, the error is only for the string with realloc =: it is impossible to convert "void " to "discipliny *" "void", explicit cast required. Changed to (discipliny*)malloc(sizeof(disciplina)*n) , the same errors. - reger

1 answer 1

Oh

 #include <stdio.h> #include <stdlib.h> typedef struct {char nazvanie[20], otchet[6]; int vse_chasy; } discipliny ; int main() { int i, n=1; FILE *file; file = fopen("a.txt", "r"); discipliny *disciplina; disciplina = (discipliny*)malloc(sizeof(discipliny)*n); i=0; while(!feof(file)) { fscanf(file,"%19s", disciplina[i].nazvanie); fscanf(file," %d\n", &(disciplina[i].vse_chasy)); i++; disciplina = realloc(disciplina, sizeof(discipliny)*(i+1)); n=i; } fclose(file); return 0; } 

Ask questions.

  • The whole point was that the reading was performed in the function void read(discipliny *disciplina, int *n) which is declared in the maine read(disciplina, &n); In this case, it gives an error converting "void" to "discipliny". If you do everything in Maine, as you suggested, then everything is fine. Thanks for the answer, I threw everything in Maine. - reger
  • @reger: It's impossible for the code to work in main and not work in another function, unless you are making some wild mistake. - VladD
  • @VladD If you compare my text and the @reger text, you will find a number of differences. Well, the fact that some things in C are simply not translated (I had to add a typedef) is fine. C, C ++ - tolerated. But sizeof(disciplina) is already an obvious mistake. Variable i , which for realloc db. one more. Maybe something else missed. - alexlz
  • 2
    @reger, throw in main, because the function does not compile, this is a bad decision. You have some very simple error related to the fact that the name of the variable and the type are very similar. In general, I almost always write variable-name in sizeof () (in malloc / realloc * pointer-name), not type. By the way, increasing the size of an array by 1 element at each iteration is not a good idea. It is better to immediately allocate a reasonable amount of memory, for example, a page (or several) and, when it is exhausted, add another page, etc., and at the end do realloc, reducing the size of the array to the real value. - avp
  • @avp In principle, this is correct, but in this particular case, @reger might not have noticed the error. But the variable name / structure class name (C ++) is yes. And specifying * var in sizeof instead of type is a matter of personal preference and debatable. - alexlz