这是针对于字符串的指针数组做出的总结
主要有三个例子
代表了不同的写法
我的编译器是Visual Studio 2019 Community
这是第一段标准的C语言写法
#include <iostream> #include<stdlib.h> #include<stdio.h> #include<math.h> using namespace std; int main() { int i; char p[12][30] = { "January","February","March","April","May","June","July","August","September","October","November","December" }; printf("please input your number of month:"); cin >> i; char* a; a = p[12]; printf("\n"); printf("The month is:%s\n", *(p+i - 1)) return0; }
第二段C++的写法
#include <iostream> #include<stdlib.h> #include<stdio.h> #include<math.h> using namespace std; int main() { int i; const char* p[12] = { "January","February","March","April","May","June","July","August","September","October","November","December" }; printf("please input your number of month:"); cin >> i; printf("\n"); printf("The month is:%s\n", p[i-1]); return0; }
第三段也是C++的写法
#include <iostream> #include<stdlib.h> #include<stdio.h> #include<math.h> using namespace std; int main() { int i; const char* p[12] = { "January","February","March","April","May","June","July","August","September","October","November","December" }; printf("please input your number of month:"); cin >> i; printf("\n"); printf("The month is:%s\n", *p+i-1); return0; }