Access memory allocated on a pointer

1

If I have a vector of pointers of type *p[tamanho] , where each position will be occupied by p[tamanho] = malloc(10*sizeof(int)) , how do I access each position of that vector allocated with malloc ?

    
asked by anonymous 26.10.2015 / 04:53

1 answer

2

How about this?

int i, j;
for (i = 0; i < tamanho; i++) {
    for (j = 0; j < 10; j++) {
        printf("%d ", p[i][j]);
    }
}

Note that in the first pair of brackets, we access a position in the array, which results in a pointer. With the second pair of brackets, we access the position of the array from the resulting pointer.

    
26.10.2015 / 07:49