编写一程序,输入月份,输出该月的英文名。例如,输入“3”,则输出March,要求用指针实现。(两种方法,指针和指针数组)

1.用两个指针,一个代表行,一个代表列

#include
#include
#include

int main(){
	int m;
	scanf("%d",&m);
	char *hang,lie = 0;
	char s[12][50] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
	int i;
	hang = &s[m-1][0];
	int len = strlen(s[m-1]);
	printf("英文是:");
	for(i = 0; i < len; i++){
		printf("%c",*(hang+lie));
		lie++;
	}
	return 0;
}

 2.指针数组

用指针指向行

注意,给一个二维数组赋值时,只有第一个能为空

#include
#include
#include

int main(){
	int m;
	scanf("%d",&m);
	char *p;
	char s[][20] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
	p = &s[m-1][0];
	printf("英文是:%s",p);
	return 0;
}

你可能感兴趣的:(指针,c语言)