I need to use pointer Arithmetic to iterate through a 2D array and print out the coordinate points inserted in main. I can't seem to get this right...
`
#include <stdio.h>
void printTriangle(const int printPoints[3][2]);
int main()
{
const int points[3][2];
printf("Enter point #1 as x and y: ");
scanf("%d %d", *(points + 0), *(points + 1));
printf("Enter point #2 as x and y: ");
scanf("%d %d", *(points + 2), *(points + 3));
printf("Enter point #3 as x and y: ");
scanf("%d %d", *(points + 4), *(points + 5));
//printf("%d", points[2][0]);
printf("\nStarting Triangle: ");
printTriangle(points);
}
void printTriangle(const int printPoints[3][2])
{
int *ptr;
ptr = printPoints;
int i = 0;
int j = i + 1;
for (i = 0; i<6;)
{
printf("(%d, %d)", *(ptr + i), *(ptr + i + 1));
i += 2;
}
}
You are trying to change the array so it must be defined without the qualifier const.
As for the pointer arithmetic then for example the values of the array can be entered the following way
The function also uses pointers incorrectly
The parameter of the function is adjusted to type
int ( * )[2]
that you are trying to assign to a pointer of typeint *
. There is no implicit conversion from the one type to another.If you want to declare a local pointer within the function then the declaration should look like