我可以使用指针打印出来int数组数组一样,字符串数组? [英] Can I use pointer to print out arrays of int array just like arrays of string?

查看:122
本文介绍了我可以使用指针打印出来int数组数组一样,字符串数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想打印出像字符串数组:

 字符juices_A [] [12] = {火龙果,waterberry,sharonfruit};

我可以简单地使用 juices_A [0] juices_A [1] juices_A [3] ,分别作为指向字符串火龙果waterberrysharonfruit,并打印出来。

但如果我想打印出来int数组数组这样的:

  int数据[3] [3] = {{1,2,3},{4,5,6},{7,8,9}};

我不能简单地用数据[0] 数据[1] 数据[2] 为指针 {1,2,3} {4,5,6} {7,8,9} ,我需要采用打印出来一个更复杂的方式。我需要使用内循环回路。所以,我想知道为什么我不能在INT情况下使用指针?我读的书的数组变量就像指针.. 所以我假设int数组变量数据[0],数据[1],数据[2] 也像指针...

 的#include<&stdio.h中GT;诠释主(){    INT I_A = 0;
    炭juices_A [] [12] = {火龙果,waterberry,sharonfruit};
    为(; I_A 3;; I_A ++)
        的printf(%S;juices_A [I_A]);
    卖出期权();    INT i_B = 0;
    int数据[3] [3] = {{1,2,3},{4,5,6},{7,8,9}};
    为(; i_B 3;; i_B ++){
        INT i_C = 0;
        为(; i_C 3;; i_C ++)
            的printf(%i中,数据[i_B] [i_C]);
        的printf();
    }    返回0;
}

结果是:

 火龙果; waterberry; sharonfruit;
1,2,3; 4,5,6; 7,8,9,;


解决方案

您可以像使用指针访问数组,反之亦然。

下面是从下面的程序的输出:

 火龙果; waterberry; sharonfruit;
{{1,2,3,},{4,5,6,},{7,8,9,},}

下面是您code的修改:

 的#include<&stdio.h中GT;
#定义ROW_WIDTH 3
#定义TABLE_HEIGHT 3
诠释主要(无效){    字符*果汁[] = {火龙果,waterberry,sharonfruit};
    int数据[TABLE_HEIGHT] [ROW_WIDTH] = {{1,2,3},{4,5,6},{7,8,9}};    对于(INT N = 0; N< ROW_WIDTH; N ++)
        的printf(%S;,果汁[N]);    为int * IP =(INT *)的数据;    的printf(\\ N {);
    的for(int i = 0; I< TABLE_HEIGHT;我++){
       的printf({);
       对于(INT J = 0; J< ROW_WIDTH; J ++){
           的printf(%d个,* IP +((我* ROW_WIDTH)+ J));
       }
       的printf(});
    }
    的printf(} \\ n);
    返回0;
}

If I want to print out array of string like:

char juices_A[][12] = { "dragonfruit", "waterberry", "sharonfruit", };

I can simply use juices_A[0],juices_A[1],juices_A[3], respectively as a pointer to string "dragonfruit", "waterberry", "sharonfruit", and print them out.

But what if I want to print out array of int array like:

int data[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

I can not simply use data[0],data[1],data[2] as pointer to { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, I need to adopt a more complicated way to print them out. I need to use loop inside loop. So I want to know why I can not use pointer in the int situation? I read on book "Array variables are like pointers.." so I assume int array variable data[0],data[1],data[2] are also like pointers...

#include <stdio.h>

int main() {

    int i_A = 0;
    char juices_A[][12] = { "dragonfruit", "waterberry", "sharonfruit", };
    for (; i_A < 3; i_A++)
        printf("%s;", juices_A[i_A]);
    puts("");

    int i_B = 0;
    int data[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    for (; i_B < 3; i_B++) {
        int i_C = 0;
        for (; i_C < 3; i_C++)
            printf("%i,", data[i_B][i_C]);
        printf(";");
    }

    return 0;
}

the result is:

dragonfruit;waterberry;sharonfruit;
1,2,3,;4,5,6,;7,8,9,;

解决方案

You can use access arrays like pointers and vice versa.

Here's the output from the program below:

dragonfruit;waterberry;sharonfruit;
{ { 1, 2, 3, }, { 4, 5, 6, }, { 7, 8, 9, }, }

Here's a modification to your code:

#include <stdio.h>
#define ROW_WIDTH 3
#define TABLE_HEIGHT 3
int main(void) {

    char *juices[] = { "dragonfruit", "waterberry", "sharonfruit", };
    int data[TABLE_HEIGHT][ROW_WIDTH] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    for (int n = 0; n < ROW_WIDTH; n++) 
        printf("%s;", juices[n]);

    int *ip = (int *)data;

    printf("\n{ ");
    for (int i = 0; i < TABLE_HEIGHT; i++) {
       printf("{ ");
       for(int j = 0; j < ROW_WIDTH; j++) {
           printf("%d, ", *ip + ((i * ROW_WIDTH) + j));
       }
       printf("}, ");
    }
    printf("}\n");
    return 0;
}

这篇关于我可以使用指针打印出来int数组数组一样,字符串数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆