C语言学习3

二维数组:类型说明符 数组名[行数][列数]; 
int a[3][4];/*定义一个整形二维数组a,有3行4列共12个元素分别为:
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
*/
char arry[10][10];//定义一个字符型二维数组arry,有10行10列,依次为arry[0][0]~arry[9][9]共100个元素
       二维数组初始化  
int a[3][4]={{1,2,3,4},{10,20,30,40},{100,200,300,400}};//定义一个三行四列的二维数组,按行赋值
int a[3][4]={1,2,3,4,10,20,30,40,100,200,300,400};//定义一个三行四列的二维数组并对其中的12(3*4)个元素进行赋值
字符数组也可采用字符串常量的赋值方式,例如: char a[]={"china"};

strcpy()函数:拷贝一个字符串到另一个字符串数组中
将一个字符串指针拷贝到一个字符串数组中 ,并输出拷贝后目标字符串
#include <string.h>
 #include <stdio.h>
 int main(void) {
 char string[10];
 char *str1 = "www.dotcpp.com";
 strcpy(string, str1);
 printf("%s\n", string);
 return 0;
}
运行结果:

www.dotcpp.com

strcat()函数:将一个字符串拼接在目标字符串的后面
将字符串经过拼接处理,形成新的字符串,并输出新字符串
#include<string.h>
 #include<stdio.h>
 int main(void){
 char destination[25]={"I love"};
 char *blank = " ", *c = "www.dotcpp.com";
 strcat(destination, blank);
 strcat(destination, c);
 printf("%s\n", destination);
 return 0;
 }
运行结果:

I love www.dotcpp.com


strcmp()函数:比较两个字符串的大小(从第一个字符开始依次比较ASCLL码值)
char *buf1 = "www.dotcpp.com", *buf2 = "WWW.DOTCPP.COM";
int ptr = strcmp(buf2, buf1);
 str1 > str2 , 返回 1;
 str1 < str2 , 返回 -1;
 str1 == str2 , 返回 0;

strchr()函数:查找字符串中第一个出现的指定字符的位置
查找字符串string中指定字符c的首次出现的位置
#include <string.h>
 #include <stdio.h>
 int main(void) {
 char string[15]; //定义字符数组
  char *ptr, c = 'c'; 
  strcpy(string, "www.dotcpp.com"); //复制字符串
  ptr = strchr(string, c); //查找字符出现的第一个位置
  if (ptr) {
  printf("The character %c is at position: %d\n", c, ptr-string);
  }else{
   printf("The character was not found\n");
  }
 return 0;
}
运行结果:

The character c is at position: 7

strcmpi()函数:比较两个字符串的大小;比较两个字符串的大小,但是不区分大小写

struct结构体的定义和使用
#include<stdio.h>
 #include<string.h>
  struct _INFO
{
         int num;
          char str[256];
  };
  int main()
  {
        struct _INFO A;
          A.num = 2014;
          strcpy(A.str,"Welcome to dotcpp.com");
          printf("This year is %d %s\n",A.num,A.str);
          return 0;
  }

结构体数组的使用
struct address
{
        char name[30];
        /*姓名,字符数组作为结构体中的成员 */
        char street[40]; /*街道*/
        unsigned long tel; /*电话,无符号长整型作为结构体中的成员 */
        unsigned long zip; /*邮政编码*/
}student[3]={
{"Zhang","Road NO.1",111111,4444},
{"Wang"," Road NO.2",222222,5555},
{"Li"," Road NO.3",333333,6666}
};
结构体指针及具体操作
#include<stdio.h>
struct address
{
        char name[30];
        /*姓名,字符数组作为结构体中的成员 */
        char street[40]; /*街道*/
        unsigned long tel; /*电话,无符号长整型作为结构体中的成员 */
        unsigned long zip; /*邮政编码*/
};
int main()
{
    struct address A[3]={{"Zhang","Road NO.1",111111,4444},
    {"Wang"," Road NO.2",222222,5555},
{"Li"," Road NO.3",333333,6666}};
    struct address *p;
    p=&A[0];
    printf("%s %s %u %u\n",p->name,p->street,p->tel,p->zip);
    return 0;    
}
union共用体的定义和使用
共用体变量中起作用的成员是最后一次存放的成员,在存入一个新成员后,原有成员就失去作用。 共用体变量的地址和它的各成员的地址都是同一地址。
#include<stdio.h>
union INFO
{
        int a;
        int b;
        int c;
};
int main()
{
        union INFO A;
        A.a=1;
        A.b=2;
        A.c=3;
        printf("a:%d\n",A.a);
        printf("b:%d\n",A.b);
        printf("c:%d\n",A.c);
        return 0;
}
   运行结果为:
a:3
b:3
c:3
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值