详解C语言中的字符串数组

时间:2022-04-20 02:30:47

在C语言当中,字符串数组可以使用: char a[] [10]; 或者 char *a[]; 表示

第一种表示方式固定了每个字符串的最大大小。第二种没有字符串的大小限制。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <string.h>
//该程序的功能是 输入阿拉伯数字的月份数 输出英文月份
int main()
{
  //一个字符串数组 它的下标代表英文月份的阿拉伯数字
  char *month[] = {"January","February","March","April",
      "May","June","July","August","September","October",
      "November","December"};
  char *curMonth = month[0];
  int mon = 0;
  printf("请输入阿拉伯数字的月份数:");
  scanf("%d",&mon);
  switch(mon){
    case 1: curMonth = month[0];  break;
    case 2: curMonth = month[1];  break;
    case 3: curMonth = month[2];  break;
    case 4: curMonth = month[3];  break;
    case 5: curMonth = month[4];  break;
    case 6: curMonth = month[5];  break;
    case 7: curMonth = month[6];  break;
    case 8: curMonth = month[7];  break;
    case 9: curMonth = month[8];  break;
    case 10: curMonth = month[9];  break;
    case 11: curMonth = month[10]; break;
    case 12: curMonth = month[11]; break;
    default : curMonth = "No this month";
  }
  if( strcmp(curMonth,"No this month") == 0 ){
    printf("没有这个月份\n");
  }else{
    printf("当前月份为:%s\n",curMonth);
  }
  return 0;
}

总结

以上所述是小编给大家介绍的C语言中的字符串数组,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://blog.csdn.net/qq_34804120/article/details/80516245