lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

 

c:c语言数字转化为字符串以及指向三维数组的指针

 

 

 

 

 



[root@rockylinux tmp]# cat pointer_array.c 

[root@rockylinux tmp]# cat pointer_array.c 
/*      date: 2022-07-01
 *
 *
 *      知识点:数字转化为字符串:sprintf();
 *
 *
 *      三维数组的指针:*pt_three[second_dimension][third_dimension] = three_dimension_array_name;
 *      三维数组的使用: *(   *(  *( pt_three + first_dimension_offset ) + second_dimension_offset  ) + third_dimension_offset    )
 *
 *      三维数组的指针移动:
 *                      三维数组在第一维度方向的指针移动: *(pt_three + i);
 *                      三维数组在第二维度方向的指针移动: *(*pt_three + j);
 *                      三维数组在第三维度方向的指针移动: *(**pt_three + k);
 *
 *
 * */



#include
#include
#include


void msg()
{

        int one[5]={1,2,3,4,5};
        int two[3][4]={10,11,12,13,20,21,22,23,30,31,32,33};
        int three[3][4][5]={0};


        // three[3][4][5] initalizes.
         printf("\npt_three: testing\n");
        for(int i=0; i<3; i++)
        {
                for(int j=0; j<4; j++)
                {
                        for(int k=0; k<5; k++)
                        {
                                // 定义临时变量
                                char tmp_char[10];
                                int tmp_int=0;
                                // 数字转为字符串
                                sprintf(tmp_char, "%d%d%d",i+1,j+1,k+1);
                                 printf("tmp_char=%s\t", tmp_char);
                                // 字符转化为数字
                                tmp_int = atoi(tmp_char);
                                 printf("tmp_int=%d\t", tmp_int);
                                // 数字给数组赋值
                                three[i][j][k]=tmp_int;
                                // 数据调试
                                 printf("three[%d][%d][%d]=%d\n", i,j,k, three[i][j][k]);
                                // 输出换行控制符
                                 if(3==4){printf("\n");}
                        }
                }

        }
        printf("\n");


        int *ptone = one;
        int (*pttwo)[4] = two;
        int (*ptthree)[4][5]=three;


        printf("\npointer_one: display\n");
        for(int i=0; i<5; i++)
        {
                printf("pt_one[%d]=%d\t",i, *(ptone+i));
                if(4==i)printf("\n");
        }
        printf("\n");


        printf("\npointer_two: display\n");
        for(int i=0; i<3; i++)
        {
                for(int j=0; j<4; j++)
                {
                        printf("pt_two[%d][%d]=%d\t", i, j, *(*(pttwo+i)+j) );
                        if(3==j) printf("\n");
                }
        }
        printf("\n");


        printf("\npt_three: display\n");
        for(int i=0; i<3;i++)
        {
                for(int j=0; j<4; j++)
                {
                        for(int k=0; k<5; k++)
                        {
                                printf("pt_three[%d][%d][%d]=%d\t", i,j,k, *(*(*(ptthree+i)+j)+k));
                                if(4==k) printf("\n");
                        }
                }
        }
        printf("\n\n");


        // 三维数组第三维度方向的指针移动
        ptthree = three;
        printf("three_dimensions: three_dimensions_third_dimension_movement\n");
        printf("pt_array[2][0][0]=%d\n", *( **(ptthree+2)+0) );
        printf("pt_array[2][0][1]=%d\n", *( **(ptthree+2)+1) );
        printf("pt_array[2][0][2]=%d\n", *( **(ptthree+2)+2) );
        printf("pt_array[2][0][3]=%d\n", *( **(ptthree+2)+3) );
        printf("pt_array[2][0][4]=%d\n\n\n", *( **(ptthree+2)+4) );
}


int main(int argc, char *argv[])
{
        msg();

        return 0;
}
[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]# ./pointer_array 

pt_three: testing
tmp_char=111    tmp_int=111     three[0][0][0]=111
tmp_char=112    tmp_int=112     three[0][0][1]=112
tmp_char=113    tmp_int=113     three[0][0][2]=113
tmp_char=114    tmp_int=114     three[0][0][3]=114
tmp_char=115    tmp_int=115     three[0][0][4]=115
tmp_char=121    tmp_int=121     three[0][1][0]=121
tmp_char=122    tmp_int=122     three[0][1][1]=122
tmp_char=123    tmp_int=123     three[0][1][2]=123
tmp_char=124    tmp_int=124     three[0][1][3]=124
tmp_char=125    tmp_int=125     three[0][1][4]=125
tmp_char=131    tmp_int=131     three[0][2][0]=131
tmp_char=132    tmp_int=132     three[0][2][1]=132
tmp_char=133    tmp_int=133     three[0][2][2]=133
tmp_char=134    tmp_int=134     three[0][2][3]=134
tmp_char=135    tmp_int=135     three[0][2][4]=135
tmp_char=141    tmp_int=141     three[0][3][0]=141
tmp_char=142    tmp_int=142     three[0][3][1]=142
tmp_char=143    tmp_int=143     three[0][3][2]=143
tmp_char=144    tmp_int=144     three[0][3][3]=144
tmp_char=145    tmp_int=145     three[0][3][4]=145
tmp_char=211    tmp_int=211     three[1][0][0]=211
tmp_char=212    tmp_int=212     three[1][0][1]=212
tmp_char=213    tmp_int=213     three[1][0][2]=213
tmp_char=214    tmp_int=214     three[1][0][3]=214
tmp_char=215    tmp_int=215     three[1][0][4]=215
tmp_char=221    tmp_int=221     three[1][1][0]=221
tmp_char=222    tmp_int=222     three[1][1][1]=222
tmp_char=223    tmp_int=223     three[1][1][2]=223
tmp_char=224    tmp_int=224     three[1][1][3]=224
tmp_char=225    tmp_int=225     three[1][1][4]=225
tmp_char=231    tmp_int=231     three[1][2][0]=231
tmp_char=232    tmp_int=232     three[1][2][1]=232
tmp_char=233    tmp_int=233     three[1][2][2]=233
tmp_char=234    tmp_int=234     three[1][2][3]=234
tmp_char=235    tmp_int=235     three[1][2][4]=235
tmp_char=241    tmp_int=241     three[1][3][0]=241
tmp_char=242    tmp_int=242     three[1][3][1]=242
tmp_char=243    tmp_int=243     three[1][3][2]=243
tmp_char=244    tmp_int=244     three[1][3][3]=244
tmp_char=245    tmp_int=245     three[1][3][4]=245
tmp_char=311    tmp_int=311     three[2][0][0]=311
tmp_char=312    tmp_int=312     three[2][0][1]=312
tmp_char=313    tmp_int=313     three[2][0][2]=313
tmp_char=314    tmp_int=314     three[2][0][3]=314
tmp_char=315    tmp_int=315     three[2][0][4]=315
tmp_char=321    tmp_int=321     three[2][1][0]=321
tmp_char=322    tmp_int=322     three[2][1][1]=322
tmp_char=323    tmp_int=323     three[2][1][2]=323
tmp_char=324    tmp_int=324     three[2][1][3]=324
tmp_char=325    tmp_int=325     three[2][1][4]=325
tmp_char=331    tmp_int=331     three[2][2][0]=331
tmp_char=332    tmp_int=332     three[2][2][1]=332
tmp_char=333    tmp_int=333     three[2][2][2]=333
tmp_char=334    tmp_int=334     three[2][2][3]=334
tmp_char=335    tmp_int=335     three[2][2][4]=335
tmp_char=341    tmp_int=341     three[2][3][0]=341
tmp_char=342    tmp_int=342     three[2][3][1]=342
tmp_char=343    tmp_int=343     three[2][3][2]=343
tmp_char=344    tmp_int=344     three[2][3][3]=344
tmp_char=345    tmp_int=345     three[2][3][4]=345


pointer_one: display
pt_one[0]=1     pt_one[1]=2     pt_one[2]=3     pt_one[3]=4     pt_one[4]=5


pointer_two: display
pt_two[0][0]=10 pt_two[0][1]=11 pt_two[0][2]=12 pt_two[0][3]=13
pt_two[1][0]=20 pt_two[1][1]=21 pt_two[1][2]=22 pt_two[1][3]=23
pt_two[2][0]=30 pt_two[2][1]=31 pt_two[2][2]=32 pt_two[2][3]=33


pt_three: display
pt_three[0][0][0]=111   pt_three[0][0][1]=112   pt_three[0][0][2]=113   pt_three[0][0][3]=114   pt_three[0][0][4]=115
pt_three[0][1][0]=121   pt_three[0][1][1]=122   pt_three[0][1][2]=123   pt_three[0][1][3]=124   pt_three[0][1][4]=125
pt_three[0][2][0]=131   pt_three[0][2][1]=132   pt_three[0][2][2]=133   pt_three[0][2][3]=134   pt_three[0][2][4]=135
pt_three[0][3][0]=141   pt_three[0][3][1]=142   pt_three[0][3][2]=143   pt_three[0][3][3]=144   pt_three[0][3][4]=145
pt_three[1][0][0]=211   pt_three[1][0][1]=212   pt_three[1][0][2]=213   pt_three[1][0][3]=214   pt_three[1][0][4]=215
pt_three[1][1][0]=221   pt_three[1][1][1]=222   pt_three[1][1][2]=223   pt_three[1][1][3]=224   pt_three[1][1][4]=225
pt_three[1][2][0]=231   pt_three[1][2][1]=232   pt_three[1][2][2]=233   pt_three[1][2][3]=234   pt_three[1][2][4]=235
pt_three[1][3][0]=241   pt_three[1][3][1]=242   pt_three[1][3][2]=243   pt_three[1][3][3]=244   pt_three[1][3][4]=245
pt_three[2][0][0]=311   pt_three[2][0][1]=312   pt_three[2][0][2]=313   pt_three[2][0][3]=314   pt_three[2][0][4]=315
pt_three[2][1][0]=321   pt_three[2][1][1]=322   pt_three[2][1][2]=323   pt_three[2][1][3]=324   pt_three[2][1][4]=325
pt_three[2][2][0]=331   pt_three[2][2][1]=332   pt_three[2][2][2]=333   pt_three[2][2][3]=334   pt_three[2][2][4]=335
pt_three[2][3][0]=341   pt_three[2][3][1]=342   pt_three[2][3][2]=343   pt_three[2][3][3]=344   pt_three[2][3][4]=345


three_dimensions: three_dimensions_third_dimension_movement
pt_array[2][0][0]=311
pt_array[2][0][1]=312
pt_array[2][0][2]=313
pt_array[2][0][3]=314
pt_array[2][0][4]=315


[root@rockylinux tmp]# 
[root@rockylinux tmp]# 
[root@rockylinux tmp]#   

 

 

 

 

posted on 2022-07-01 10:47  lnlidawei  阅读(299)  评论(0编辑  收藏  举报