arrays - Malloc doesn't allocate memory in C -


i trying build function gets (**group, *count) count amount of items in array , group pointer array.

i must use **group instead of easier *group.

edit: requested ive included main() func:

int *group1, **pgroup1, count1 = 0, *pcount1; pgroup1 = &group1; printf("please enter size want array be..\n"); scanf("%d", &count1); pcount1 = &count1; buildgroup(pgroup1, pcount1);  void  buildgroup(int** group, int* count) {     int = 0, j = 0, c = *count;     group = (int**)malloc(c*sizeof(int**));     if (group == null)     {         printf("error: out of memory\n");         return 1;     }     printf("please enter %d items in array...\n", *count);     (i = 0; < *count; i++) //going through array items filled.      {         scanf("%d", &group[i]);         (j = 0; j < i; j++)         {             while (group[i] == group[j]) //checking every item if in group,if in group prompting user re-entering.              {                 printf("you've entered same value beforehand, please enter value again..\n");                 scanf("%d", &group[j]);             }         }     } } 

i don't know why malloc doesn't allocate memory needed array. on other hand, doesn't trigger if (==null) don't know doing wrong.

it seems passing function (or really, should passing function) pointer pointer variable, , should use dereference in function access pointer variable.

something this:

int *group; int count = 10; buildgroup(&group, &count); 

that means function should like

void  buildgroup(int **group, int *count) {     if ((*group = malloc(*count * sizeof(**group))) == null)     {         // failed allocate memory         return;     }      printf("please enter %d items in array...\n", *count);      (int = 0; < *count; ++i)     {         scanf("%d", *group + i);         // `*group + i` same `&(*group)[i]`          ... inner loop here...     } } 

i don't see reason count argument pointer, unless function supposed set too.


a little explanation what's going on

in main function have pointer variable, groups. want allocate memory , assign pointer memory group variable. that's easy, that's just

group = malloc(n * sizeof(*group)); 

the problem comes because want allocate memory inside function, , it's problem because when pass argument function done by value means value copied , argument variable inside function copy. modifying copy of course not modify original.

that problem solve if c pass group variable function by reference, meaning inside function argument variable reference variable in main function. unfortunately c doesn't have pass reference semantics, have pass value. can solved emulating pass reference, using pointers.

when pass pointer function, pointer passed value, , copied, trying change pointer point somewhere else fruitless change local copy of pointer. however, can change data points to, done using dereference operator *. passing pointer data done address-of operator &.

with information above, how 1 emulate passing pointer reference? other variable, using address-of operator pass pointer to pointer variable. inside function use dereference operator access original pointer variable.

more graphically seen this:

 +--------------------------------+ | &group (in main function)      | -\ +--------------------------------+   \  +--------------------------+    +-----+                                       > | group (in main function) | -> | ... | +--------------------------------+   /  +--------------------------+    +-----+ | group (in buildgroup function) | -/ +--------------------------------+ 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -