S
somenath
Hello All,
I am very confused about the output of the following program.
#include<stdio.h>
int daytab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31},
};
int day_of_year(int year,int month,int day)
{
int i;
int leap;
int (*p2)[13];
int (*p3)[2][13];
printf("\nsizeof daytab = %d\n", (int) sizeof(daytab));
printf("\nsizeof p3= %d\n", (int) sizeof(*p3));
printf("\nsizeof p3[2][3]= %d\n", (int) sizeof(p3[2][3]));
printf("\nsizeof p2= %d\n", (int) sizeof(*p2));
if (month <1 || month >12 || day <1|| day >31)
{
return -1;
}
leap = year %4 && year %100 !=0 || year %400;
p2 = &daytab[0];
p3 = &daytab;
printf("\nleap =%d\n",leap);
for (i =1 ;i<month;i++){
day += *(*(daytab+leap)+i);
//day += *(*(p2+leap)+i);
//day += *(*(*p3+leap)+i);
}
}
int main(void)
{
int day;
day = day_of_year(2012,3,3);
printf("\nDay = %d\n",day);
return 0;
}
Output of the program
sizeof daytab = 104
sizeof p3= 104
sizeof p3[2][3]= 52
sizeof p2= 52
leap =1
Day = 63
My question is how the below line getting evaluated and resulting
correct output?
day += *(*(daytab+leap)+i);
According to my understanding daytab is pointing to the whole daytab[2]
[13] array i.e it is equivalent to p3. So daytab +1 will point
beyond the daytab array so trying to de reference it will cause
undefined behaviour. But I am getting the right result. Where am I
going wrong?
Along with this I would like to verify my understanding about the p2
and p3 as well.
So p2 point to the first row of the daytab array i.e it points to
{0,31,28,31,30,31,30,31,31,30,31,30,31},
Now p2+leap will point to second row depending upon the leap value.
*(p2+leap) is pointer to one of the element of the array for example
it may point to 0 of the first row. So adding i to it will shift the
pointer in that row to the i th element. Then de-reference it result
in the actual value.
Similarly p3 will point to the whole array daytab. The type of *p3
will be equivalent to p2. So the pointer arithmetic on p3 behave same
as p2 . But I am not sure that i am right here as I get the out put
of
printf("\nsizeof p3= %d\n", (int) sizeof(*p3));
as same as
printf("\nsizeof daytab = %d\n", (int) sizeof(daytab));
I am very much confused here.
Also i am not able to understand why the output of
printf("\nsizeof p3[2][3]= %d\n", (int) sizeof(p3[2][3])); is 52
which is same as
printf("\nsizeof p2= %d\n", (int) sizeof(*p2));
I am not able correctly visualize the type of different pointers that
may be the reason for the whole confusion. Please provide some inputs
regarding this so that I can clearly understand the the pointer type.
I have read the article from the following link.
http://www.torek.net/torek/c/pa.html
After reading it i thought I have understood the different type
associated with pointer. But I am not able to understand still. It is
very much evident as I have failed to understand the behaviour of the
above program.
Thanks
Somenath
I am very confused about the output of the following program.
#include<stdio.h>
int daytab[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31},
};
int day_of_year(int year,int month,int day)
{
int i;
int leap;
int (*p2)[13];
int (*p3)[2][13];
printf("\nsizeof daytab = %d\n", (int) sizeof(daytab));
printf("\nsizeof p3= %d\n", (int) sizeof(*p3));
printf("\nsizeof p3[2][3]= %d\n", (int) sizeof(p3[2][3]));
printf("\nsizeof p2= %d\n", (int) sizeof(*p2));
if (month <1 || month >12 || day <1|| day >31)
{
return -1;
}
leap = year %4 && year %100 !=0 || year %400;
p2 = &daytab[0];
p3 = &daytab;
printf("\nleap =%d\n",leap);
for (i =1 ;i<month;i++){
day += *(*(daytab+leap)+i);
//day += *(*(p2+leap)+i);
//day += *(*(*p3+leap)+i);
}
}
int main(void)
{
int day;
day = day_of_year(2012,3,3);
printf("\nDay = %d\n",day);
return 0;
}
Output of the program
sizeof daytab = 104
sizeof p3= 104
sizeof p3[2][3]= 52
sizeof p2= 52
leap =1
Day = 63
My question is how the below line getting evaluated and resulting
correct output?
day += *(*(daytab+leap)+i);
According to my understanding daytab is pointing to the whole daytab[2]
[13] array i.e it is equivalent to p3. So daytab +1 will point
beyond the daytab array so trying to de reference it will cause
undefined behaviour. But I am getting the right result. Where am I
going wrong?
Along with this I would like to verify my understanding about the p2
and p3 as well.
So p2 point to the first row of the daytab array i.e it points to
{0,31,28,31,30,31,30,31,31,30,31,30,31},
Now p2+leap will point to second row depending upon the leap value.
*(p2+leap) is pointer to one of the element of the array for example
it may point to 0 of the first row. So adding i to it will shift the
pointer in that row to the i th element. Then de-reference it result
in the actual value.
Similarly p3 will point to the whole array daytab. The type of *p3
will be equivalent to p2. So the pointer arithmetic on p3 behave same
as p2 . But I am not sure that i am right here as I get the out put
of
printf("\nsizeof p3= %d\n", (int) sizeof(*p3));
as same as
printf("\nsizeof daytab = %d\n", (int) sizeof(daytab));
I am very much confused here.
Also i am not able to understand why the output of
printf("\nsizeof p3[2][3]= %d\n", (int) sizeof(p3[2][3])); is 52
which is same as
printf("\nsizeof p2= %d\n", (int) sizeof(*p2));
I am not able correctly visualize the type of different pointers that
may be the reason for the whole confusion. Please provide some inputs
regarding this so that I can clearly understand the the pointer type.
I have read the article from the following link.
http://www.torek.net/torek/c/pa.html
After reading it i thought I have understood the different type
associated with pointer. But I am not able to understand still. It is
very much evident as I have failed to understand the behaviour of the
above program.
Thanks
Somenath