hi how can i insert a value in an array?
i want to insert an element to an array..
in php it can be done with this..
how about in c?Code:$arrayname[] .= $value;
thanks
hi how can i insert a value in an array?
i want to insert an element to an array..
in php it can be done with this..
how about in c?Code:$arrayname[] .= $value;
thanks
Last edited by fairyjerry14; 10-12-2007 at 12:34 AM.
If it's on the stack you can't.
you'd have to resize it (providing it was allocated on the heap).
Code:char * arr = NULL; size_t elements = 10; if((arr = malloc(elements * sizeof(char))) == NULL) { /* error, exit */ } /* set the array values, oops we need 11 elements */ if((arr = realloc(arr, (elements + 1) * sizeof(char))) == NULL) { /* failed to resize array */ }else{ ++elements; arr[10] = 'v'; /* some value */ } /* free the array */ free(arr); arr = NULL;