I need the code changed to use pointer arithmetic and no [ ] subscript operators
ID: 663455 • Letter: I
Question
I need the code changed to use pointer arithmetic and no [ ] subscript operators in the print statement.
Working code:
void printFunction(int twoDarray[rows][columns]){
int y;
for(y = 0; y < 3; y++){
printf("(%d, %d)",twoDarray[y][0], twoDarray[y][1]);
}
printf(" ");
}
How do I convert this from a typical print all array elements as is above to a print function that loops through the array to print all elementst but done so that I iterate through the array using pointer arithmetic? How can I print this loop without using subscripts?
Explanation / Answer
void printFunction(int twoDarray[rows][columns]){
int (*p_1)[columns] = twoDarray;
for (; p_1 != twoDarray + rows; ++p_1) {
int *p_2 = *p1;
print("(%d, %d)",*p_2,*p_2+1)
}
printf("%n")
}