我的C测试代码如下:
#include <stdio.h>
int main(void)
{
char * str="liuwei";
char *month_name[] =
{
"January","February","March",
"April","May","June",
"July","August","Septemter",
"October","November","December"
};
int i = 0;
while(i<12)
{
printf("%s\n",month_name[i]);
i++;
}
printf("%s\n",++(*month_name));
printf("%c\n",(++(*month_name))[3]);
printf("%c\n",*++month_name[1]);
return 0;
}
下面的语句,想必有点C语言基础的都能理解,就是分别打印出所有指针指向的字符串while(i<12)
{
printf("%s\n",month_name[i]);
i++;
}
结果就是:
January
February
March
April
May
June
July
August
Septemter
October
November
December
这个倒是挺容易的。
好的,那么printf("%s\n",++(*month_name));这个语句有事什么意思呢?
看我慢慢道来:
好了,*month_name就是指向"January"这个字符串,
若用printf("%s\n",(*month_name));这个语句
则会打印出
January
也可以把*month_name理解成month_name[0];
++(*month_name)这个的意思就是指针加1,指向了字符a以及其余后面字符所构成的字符串anuary,
那么printf("%s\n",++(*month_name));
就会打印出anuary,
若是printf("%s\n",++(*month_name));
则打印的是January,然后指针再加1
应该理解了吧~哈哈!(废话,都以为像你一样啊,赶紧的!~~)
2、对于printf("%c\n",(++(*month_name))[3]);这个语句,大家看一看到有%c,应该可以猜到是输出一个字符了,没错,那倒底是输出哪一个呢?
上面已经说了*month_name以及++(*month_name)的意义,
那么再首先来看看(++(*month_name))[0]的意思吧。在这个语句中,指针又加了1,又指向了后面的字符串"nuary",加后面的[0]引用,则确定到了某一个字符,即'n'.(也可以理解为month_name[0][0],当然,此时的month_name[0]已经是"nuary"了!)。
所以printf("%c\n",(++(*month_name))[3]);就是输出字符'r'了。
3、对于printf("%c\n",*++month_name[1]);咋一看我还真不理解,不过只要功夫深,哦,是“只要锄头挥的好,哪有墙脚挖不倒?” 诶哟,扯远了!~赶紧回来!首先[]的优先级(结合律)比++来的高,所以*++month_name[1]就等同于*++(month_name[1]),又因为++的优先级比*(指针引用)来的高,所以又等同于*(++(month_name[1])),问题似乎明朗起来了,
month_name[1]就是指向"February"字符串,++month_name[1]就是指向"ebruary"字符串,在*(++(month_name[1]))当中,指针是先加的1,然后在取值(引用),所以输出的就是字符'e'了,(也可以理解为month_name[1][0],当然,此时的month_name[1]已经是"ebruary"了!不知这样理解妥不妥当~但是心里一定要理解啊!)