The problem is this:

When I first get into the addItem function and make a realloc pointer to the structure in it, and then assign values ​​to it, then after I exit the function, I see these values. When I get into the function a second time, I perform the same actions, then after that I display the values ​​after the first time, and only 0 from the second time. As planned, after each iteration I have to replenish the array with reallo, but why doesn't work.

realloc occurs in the EnterData method.

PS Don't be scared. I posted the full listing just so you never know who wants to compile the prog ...
If someone wants to compile the program and look at the results, then you need to drive 0, and then some data.

#pragma hdrstop #pragma argsused #include <stdio.h> #include <stdlib.h> #ifdef _WIN32 #include <tchar.h> #else typedef char _TCHAR; #define _tmain main #endif #define SIZE 10 struct Item{ int key; int release; char *info; }; void addItem(struct Item *,int*); void deleteItem(struct Item *,int*); void searchItem(struct Item *,int); int enterData(int*,int*,char*,struct Item*,int); int CheckKey(int ,struct Item *,int); int CheckRelease(int ,int ,struct Item *,int); int _tmain(int argc, _TCHAR* argv[]) { struct Item *table; int ctrl,n=0,i; void (*P[4])(struct Item *,int*)={addItem,deleteItem,searchItem}; table=malloc(sizeof(struct Item)); for (;;) { puts("0 add\n1 delete \n2 search \n"); scanf("%d",&ctrl); if (ctrl>5) { break; } (*P[ctrl])(table,&n); for (i=0; i<=n-1; i++) { printf("%d\n",table[i].key); } } return 0; } void addItem(struct Item *table,int *n) { int key,release; char *info=malloc(sizeof(char)*50); if (*n==SIZE) { puts("Table is full. can not continue \n"); return; } if(enterData(&key,&release,info,table,*n)) { (*n)++; table=realloc(table,sizeof(struct Item)*(*n)); table[*n-1].key=key; table[*n-1].release=release; table[*n-1].info=info; } } int enterData(int *key,int *release, char *info,struct Item *table,int n) { int a; puts("Enter key: "); scanf("%d",key); if (CheckKey(*key,table,n)==2) { puts("This item already exists. The number of releases exhausted"); return 0; } if (CheckKey(*key,table,n)==1) { puts("This item already exists. You can add 1 realese"); } puts("\n Enter release: "); do { scanf("%d",release); } while (CheckRelease(*key,*release ,table,n)==0); puts("\n Enter info(50 chars): \n"); fflush(stdin); gets(info); return 1; } int CheckKey(int key,struct Item *table,int n) { int i,cnt=0; if (n==0) { return 0; } for (i = 0; i <= n-1; i++) { if(table[i].key==key) { cnt++; } } return cnt; } int CheckRelease(int key,int release ,struct Item *table,int n) { int i; if (n==0) { return 1; } for (i = 0; i <= n-1; i++) { if(table[i].key==key) { if (table[i].release==release) { puts("Thies element have this release. Enter another release."); return 0; } return 1; } } } void deleteItem(struct Item *table,int *key) { } void searchItem(struct Item *table,int key) { } 
  • @ iluxa1810, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

You do not change the contents of the table pointer (located in main).

When calling (*P[ctrl])(table,&n); A copy of the table is passed to the function, for example, addItem() , which is changed by realloc() .

Transfer the table address and change *table . Those.

  ... (*P[ctrl])(&table,&n); ... void addItem(struct Item **table,int *n) { ... if(enterData(&key,&release,info,*table,*n)) { ... *table=realloc(*table,sizeof(struct Item)*(*n)); (*table)[*n-1].key=key; ... 

Similarly, change the rest of the code.

  • Hmm ... And it seemed to me that by passing the pointer, I can do anything with it ... Ie it turns out, if I pass a simple pointer, then I can change the values ​​only in the previously allocated memory, and to change the pointer itself, do I need a pointer to a pointer? Where would you read about it, preferably in Russian? - iluxa1810 9:09 pm
  • Probably in any normal textbook on C (for example, in K & R). - avp