0

I have an arrFillRandom function which fill an array with random numbers and prints it out. Here it is:

void arrFillRandom(int *arrayPtr, int sizeRow, int sizeColumn, int randomRange) {
    int i, j;

    arrayPtr = malloc(sizeof(int) * sizeRow * sizeColumn);

    for (i = 0; i < sizeRow; i++)
        for (j = 0; j < sizeColumn; j++)
            (arrayPtr + i)[j] = (rand() % randomRange);
    
    for (i = 0; i < sizeRow; i++) {
        printf("\n");
        for (j = 0; j < sizeColumn; j++)
            printf("%d\t", (arrayPtr + i)[j]);
    }
}

And here is my main:

int main() {
void arrFillRandom(int *arrayPtr, int sizeRow, int sizeColumn, int randomRange);

int matrix1[2][3];
int matrix2[3][2];

int *ptr1, *ptr2;
ptr1 = &matrix1[0][0];
ptr2 = &matrix2[0][0];

arrFillRandom(ptr1, 2, 3, 10);
arrFillRandom(ptr2, 3, 2, 10);

printf("\n%d", ptr1[0]);

return 0;
}

My problem is that ptr1 or ptr2 doesn't point to array which arrFillRandom filled before. In arrFillRandom function, I print out the matrix with random numbers. For example:

1, 0, 9 // first row of first matrix

0, 9, 4 // second row of first matrix

8, 2 // first row of second matrix

2, 5 // second row of second matrix

5, 5 // third row of second matrix

But in main, when I want to print first element of this matrix:

printf("\n%d", ptr1[0]);

This line should print 1, but it prints irrelevant number such as 4096.

So why I couldn't store that matrix in this array?

Community
  • 1
  • 1
sedooe
  • 3,100
  • 2
  • 22
  • 27

1 Answers1

0

Your void arrFillRandom() function allocates new memory and fills that with random numbers. arrayPtr is a local variable inside the function, and assigning a new value to it does not change the passed arguments ptr1 or ptr2.

To fill the arrays whose addresses are passed to the function, simply remove the line

arrayPtr = malloc(sizeof(int) * sizeRow * sizeColumn);

You also have to change

(arrayPtr + i)[j]

to

arrayPtr[sizeColumn * i + j]

inside that function.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Thanks for solution but I'm confused a little bit. If I don't write anything about memory, if I don't use `malloc`, how do compiler know that how many spaces should be allocated for this array? I guess, `matrix1[2][3]` and `matrix2[2][2]` is enough about that, right? When I use pointers with arrays, sometimes can't understand things easily. – sedooe May 02 '14 at 23:17
  • @lombranzo: Yes, `matrix1[2][3]` *is* already an array. Btw., you can call your function as `arrFillRandom(matrix1, 2, 3, 10);` because an array "decays" to a pointer to the first element when passed as an argument to a function. – Martin R May 02 '14 at 23:20
  • I know but in assignment, it is asked to call that function like I wrote. Thanks again. :) – sedooe May 02 '14 at 23:24
  • omg, I have exactly the same problem about matrix multiplication. I would be appreaciate if you could take a look at: [link](https://ideone.com/NEq4Zl) – sedooe May 02 '14 at 23:46
  • @lombranzo: There was still another error in your arrFillRandom function, so that the matrices where not filled correctly. See updated answer. – Martin R May 03 '14 at 06:48
  • How can I do that by using pointer offset notation? `*((*(arrayPtr+i))+j)` is this correct to do that? But when I use this, when I use asteriks(*), then some errors appear again. How should I do this? I'm really so appreciate for your patience Martin. If you suggest me some web page or video about pointers, that would be great. – sedooe May 03 '14 at 19:47