I am trying to populate an array I allocated with data recieved from a socket, but am unable to make it work properly. When i define and get data through:
uint16_t data[2048];
recv(network_socket, &data, sizeof(data), 0);
the data is recieved properly, but when I try to do it through an allocated array:
int i;
int N = 3;
int M = 2048;
int **matrix;
matrix = (int **)calloc(N, sizeof(int *));
for (i=0; i<N; i++) {
matrix[i] = (int *)calloc(M, sizeof(uint16_t));
}
for (i=0; i<N; i++) {
recv(network_socket, matrix[i], sizeof(data), 0);
}
I am sensing things go wrong at the final part. As far as I understand matrix[i]
at the end gets the pointer to the beginning of row i
and should work for recv()
but that might be where I'm going wrong here.
EDIT: When I try to printf("Number: %" PRIu16 "\n", matrix[a][b]);
I get some whacko large number in the second part, wheras printf("Number: %" PRIu16 "\n", data[a]
); works fine in the first.