二维数组

a[3][4]

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]

地址概念

#include<stdio.h>
int main()
{
        int i,j;
        //int a[3][4];  //3行4列的二维数组,未初始化
        int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};  //对所有元素初始化
        //int a[3][4]={1,2,3,4,5};   //对部分元素初始化
        //int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};  //可以不写行,但一定要写列
        for(i=0;i<3;i++)
        {
                for(j=0;j<4;j++)
                {
                        printf("%d ",a[i][j]);
                }
                printf("\n");
        }
        printf("%p\n",&a[0][0]);   //第一个元素的地址
        printf("%p\n",a);   //数组名
        printf("%p\n",&a);   //
        printf("%p\n",a[0]);   //
        printf("%p\n",&a[0]);   //
        printf("************\n");
        printf("%p\n",&a[0][0]+1);   //单位是一个元素,4个字节
        printf("%p\n",a+1);   //数组名,数组首行地址
        printf("%p\n",&a+1);   //数组的地址,单位是一个数组
        printf("%p\n",a[0]+1);   //数组首行首元素的地址,单位是一个元素
        printf("%p\n",&a[0]+1);   //数组首行地
        printf("###########\n");
        printf("%p\n",a[1]+1);  //第2二行第一个元素+一个元素
        printf("%p\n",&a[1][1]);//a[1][1]地址
        printf("%p\n",*(a+1)+1);//第二行第二个元素地址
        printf("%p\n",(a+5));//第六行地址,出错
        printf("************\n");
        printf("%d\n",*(a[1]+1));  //第2二行第一个元素+一个元素
        printf("%d\n",*(&a[1][1]));//a[1][1]地址
        printf("%d\n",*(*(a+1)+1));//第二行第二个元素地址
        printf("%d\n",*((a+5)));//第六行地址,出错
        return 0;
}

运行

1 2 3 4 
5 6 7 8 
9 10 11 12 
0xbfdffdcc
0xbfdffdcc
0xbfdffdcc
0xbfdffdcc
0xbfdffdcc
************
0xbfdffdd0
0xbfdffddc
0xbfdffdfc
0xbfdffdd0
0xbfdffddc
###########
0xbfdffde0
0xbfdffde0
0xbfdffde0
0xbfdffe1c
************
6
6
6
-1075839460

字符数组

#include<stdio.h>

int main()
{
        char a[5]={'h','e','l','l','o'};
        int i;

        for(i=0;i<5;i++)
        {
                printf("%c",a[i]);
        }
}

运行

[root@localhost 28]# ./2-字符数组 
hello

 

strcpy字符串拷贝函数

#include<string.h>

int main()
{
        char a[32]={0};//对字符数组初始化,相当于char a[32]=]
        char b[32]="helloworld";
        char c[32]="123451234512345";

        strcpy(a,b);
        strcpy(c,b);
        printf("%s\n",a);
        printf("%s\n",c);

}

运行

[root@localhost 28]# ./3-字符处理函数 
helloworld
helloworld

 

strcat链接函数

#include<stdio.h>
#include<string.h>

int main()
{
        char a[32]={0};
        char b[32]="helloworld";
        char c[32]="123451234512345";

        //strcpy(b,c);//字符串连接函数,把字符串c连接到字符串b后面,b的空间要足够大
//      printf("%s\n",b);
        if(strcmp(b,c)>0)
        {
                printf("b>c\n");
        }
        if(strcmp(b,"hellnworld")>0)
        {
                printf("b>hellnworld\n");
        }
        return 0;
}

运行

[root@localhost 28]# ./4-连接函数 
b>c
b>hellnworld

自定义函数,函数调用

#include<stdio.h>

void swap(int *x,int *y);//如果调用函数写在main函数后,需在此写函数声明

void print()
{
        printf("helloworld\n");
}

int add(int x,int y)  //x,y是形参,实参和形参个数相同,类型相同,顺序相同,名字可以不同
{                       //2.给形参分配空间(栈空间)
        int sum;          //3.传参(值传递,地址传递)

        sum=x+y;      //4.执行函数体

        return sum;    //5.返回
}                 //6.释放空间(栈空间)

int main()
{
        int a=1,b=2;
        int result;

        print();
        result=add(a,b);  //a,b是实参,实参不要加类型
        printf("%d\n",result);
        swap(&a,&b);//当设计修改实参值时需要传地址
        printf("a=%d,b=%d\n",a,b);
        return 0;
}

void swap(int *x,int *y)
{
        int temp=*x;
        *x=*y;
        *y=temp;
}

运行

[root@localhost 28]# ./5-自定义函数 
helloworld
3
a=2,b=1

 

关键字 extern作用

1.c

int num=100;

int main()
{
        print();
        return 0;
}

2.c

#include<stdio.h>

extern int num;  //声明外部变量,告诉边一起,num在其他文件中被定义
                 //声明:不用分配空间
                                 //定义:分配空间
void print()
{
        printf("%d\n",num);
}

编译运行

[root@localhost extern]# gcc 1.c 2.c -o main
[root@localhost extern]# ./main
100

 

关键字static静态变量

#include<stdio.h>

void f()
{
        int a=0;
        a++;
        printf("%d\n",a);
}

void g()
{
        static int a=0;
        a++;
        printf("%d\n",a);
}
int main()
{
        int i;

        for(i=0;i<5;i++)
        {
                f();
        }

        int j;

        for(j=0;j<5;j++)
        {
                g();
        }
        return 0;
}

运行

[root@localhost 28]# ./6-static 
1
1
1
1
1
1
2
3
4
5

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值