M
mdh
May I ask two questions relating to this exercise? ( asking to repeat
the example using pointers)
Given a 2-dimensional array:
char a[2][13] ( which is initialized in the exercise to represent days
of the month in a leap/non-leap year)
Part of the answer ( in Tondo and Gimpel) includes "assigning" a char
pointer to the correct "row" thus:
char *p
p = a[leap]; ( with "leap" either 0 or 1);
Question.
If "a" is "converted" to a pointer to element 0 of row 1, then a[1]
points p to element 0 of row 2? Is this the correct way of visualizing
this?
2nd question involves pointer arithmetic.
p is advanced using *++p.
In order to calculate the difference between a[leap] and the current
position of "p" the expression
*pmonth = p - *(a + leap ) is used. ( again, "leap" is 0 or 1)
( *pmonth is a ptr of type integer declared as such in the parameter
of the function)
Question:
I can see how p represents an address of the "pth" element of "a" and
that this should simply be the difference between 2 pointers.
However, the syntax
*(a + leap) is the one that is confusing to me. I thought the "*" is
the dereferencing operator, and as such, it should be (a + p) and
not *(a+p).
thanks in advance.
the example using pointers)
Given a 2-dimensional array:
char a[2][13] ( which is initialized in the exercise to represent days
of the month in a leap/non-leap year)
Part of the answer ( in Tondo and Gimpel) includes "assigning" a char
pointer to the correct "row" thus:
char *p
p = a[leap]; ( with "leap" either 0 or 1);
Question.
If "a" is "converted" to a pointer to element 0 of row 1, then a[1]
points p to element 0 of row 2? Is this the correct way of visualizing
this?
2nd question involves pointer arithmetic.
p is advanced using *++p.
In order to calculate the difference between a[leap] and the current
position of "p" the expression
*pmonth = p - *(a + leap ) is used. ( again, "leap" is 0 or 1)
( *pmonth is a ptr of type integer declared as such in the parameter
of the function)
Question:
I can see how p represents an address of the "pth" element of "a" and
that this should simply be the difference between 2 pointers.
However, the syntax
*(a + leap) is the one that is confusing to me. I thought the "*" is
the dereferencing operator, and as such, it should be (a + p) and
not *(a+p).
thanks in advance.