I need to rotate a matrix and call the funcion without using []
, I can't even think on a solution.
void _90DegClockwise(int *pS, int row, int col) {
for (int i = 0; i < row; ++i)
{
for (int j = i + 1; j < col; j++) {
int temp;
temp = (int)(pS);
*((int*)((pS + i) + j)) = (int*)((pS + j) + i);
(int*)((pS + j) + i) = temp;
}
}
}
I don't know how to insert a value to matrix or how to swap
To solve this, you need to understand how arrays work in C.
Lets say you have a 3 * 3 matrix, that is declared like this:
int matrix[3][3];
While you imagine this to be a square like this:
+---+---+---+
| 0 | 1 | 2 |
+---+---+---+
| 3 | 4 | 5 |
+---+---+---+
| 6 | 7 | 8 |
+---+---+---+
For the computer it is a consecutive "line" in memory looking like this:
+---+---+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+---+---+---+---+---+
^
|____matrix
And variable matrix
holds the address of the first cell
So, if you want to access any cell without using the []
operator, you need to calculate the address of that cell.
Lets do this for the middle cell:
matrix[1][1];
This is the second cell of the second row, so you need to add the width of the first row to the start of the matrix, and then add one more cell from the start of the second row. And you want to derefernce the address to get to the value like so:
*(matrix + (3 * 1) + 1);
What if we wanted the middle cell of the third row? Same thing, but add width of two rows:
*(matrix + (3 * 2) + 1);
To sum up: if you want to access cell x in row y of the matrix you would calculate its address like this:
*(matrix + (with * y) + x);
You're mixing up the pointer and the dereferenced value in the 3 lines
In the first line temp = (int)(pS);
you're assigning the pointer value to temp
instead of the value that the pointer points to . Similarly you're also assigning the pointer value to the memory location like this *((int*)((pS + i) + j)) = (int*)((pS + j) + i);
which makes no sense.
Then in the last line (int*)((pS + j) + i) = temp;
doesn't work because you're assigning a value to an address instead of a memory location
Remember to use *
to dereference a pointer to get the variable, ie the memory location the pointer points to. In C a[b]
is equivalent to *(a + b)
so just replace all those occurrences. I don't know why you do it "correctly" with *((int*)((pS + i) + j))
but didn't apply that to others
Even then you're calculating the index incorrectly. *((int*)((pS + i) + j))
is just pS[i + j]
which is not the correct item you want. If you pass the 2D array as a 1D array you need to calculate the real index like this pS[i + j*width]
So to do
temp = pS[i + j*width];
pS[i + j*width] = pS[j + i*width];
pS[j + i*width] = temp;
just change it to
temp = *(pS + i + j*width);
*(pS + i + j*width) = *(pS + j + i*width);
*(pS + j + i*width) = temp;
You should enable all compiler warnings. They're very helpful and help you solve most of the above problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.