Thursday, 15 May 2014

c - Malloc for a single element of a void pointer array -



c - Malloc for a single element of a void pointer array -

i've work generic types. int,float,double,string.

let's see strings.

my array defined in string menu in way.

char** arr = (char**)malloc(number_of_strings * sizeof(char*));

now have re-create bst of strings in array. calling iterative function

palbero* a pointer bst node.

i passed 0.

size sizeof(type).

copy callback function based on types copy.

void* copia_albero_in_array_ordinato_iter(palbero* a, void* arr, int* i, int size, fncopy copy) { pstack* st = null; palbero* t = a; void* elemento; while (st || t) { if (t) { st = push(st, t); t = t->sx; } else { t = top(st); st = pop(st); elemento = malloc(size); copy(elemento, t->val); copia_dato(arr, *i, elemento, 0, sizeof(elemento)); free(elemento); t = t->dx; *(i) = *(i) + 1; } } homecoming arr; } void copia_dato(void* dest, int d_pos, void *source, int s_pos, int dim) { void *dst_addr = dest + (d_pos * dim); void *src_addr = source + (s_pos * dim); memcpy(dst_addr, src_addr, dim); }

i'm pretty new c, have allocated memory array, how allocate single cell of string within it? printing array works fine. give thanks in advance , sorry if i'm missing post something.

[update comment]

doing

arr[i] = ...

gives me:

array subscript not integer

doing

arr[*i] = ...

gives me:

[warning] dereferencing `void *' pointer, 4 invalid utilize of void look

[warning] dereferencing `void *' pointer, 4 invalid utilize of void look

if declare

..., void * arr, ...

arr pointer void, applying [] operator arr implicit dereferencing , lead object of type void. latter not exist. that's why compiler complains.

c arrays pointers

No comments:

Post a Comment