0

Here are my two structures :

int *data = malloc(sizeof(int) * 512 * 512);
int **sortedData = malloc(sizeof(int) * 512);

The data var contains integers from a file. sortedData is supposed to be a 2D version of data. Here is how I initialize it :

for (int i = 0; i < dimensions[0]; i++)
    {
        sortedData[i] = (data + i * 512);
    }

For testing the values I use :

    printf("%d\n", data[0]);
    printf("%d\n", sortedData[0][0]);`

The problem is that the outcome of the second printf is an address, so the values are :

81
44766304

I tried to print the value of (data + i * 512) like this :

for (int i = 0; i < dimensions[0]; i++)
    {
        printf("%d\n", data + i * 512);
        sortedData[i] = (data + i * 512);
    }

And here is the thing : The printf returns an address for each i, but now, the outcome of the program is what I am looking for :

81
81

I can't really understand how the printf would change the returned value and why my initial solution is wrong.

EDIT 1 : It still doesn't work after changing sortedData from

... = malloc(sizeof(int) * 512);

To

... = malloc(sizeof(int *) * 512);
Neodia
  • 1
  • 2

0 Answers0