简体   繁体   中英

Can I use pointer to print out arrays of int array just like arrays of string?

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;
}

Strings:

-------------------------
|h | e | l | l | o | \0 |
-------------------------
|w | o | r | l | d | \0 |
------------------------

As you see this array can be stored in a 2D char array

char a[2][6];

Now a[i] gives the starting address of each row and %s prints out a string until a \\0 is enountered so you can just pass the address of each row and get the contents of the row. Just like

char *p = "hello";
printf("%s\n",p);

Note: Again if you want to read the character at a particular location you need to specify a[row][col]

Whereas

Integers:

----------------
|1 | 2 | 3 | 4 |
----------------
|5 | 6 | 7 | 8 |
----------------

Now you can have a array like

int b[2][4];

b[i] will give you the starting address of each row but in order to get the value stored in a particular location you need to specify value with b[row][col] .

The difference is the sentinel condition for valid C strings is \\0 and is it being used to access each row which is not the case for integers.

so I assume int array variable data[0],data[1],data[2] are also like pointers

Yes they are pointers to each row but you need to dereference the pointer to get the value right? So if data[0] is a pointer then do data[0][column]

The char juices_A[][12] = { "dragonfruit", "waterberry", "sharonfruit" }; whenever the data is accessed it prints till \\0 is reached, as strings are considered only till \\0 . So when we use juices_A[0] it points to start of first data till \\0 is reached.But to get an individual char we need to mention both the dimensions of the array.

char juices_A[1][2] to access 't' in waterberry

But this is not the case with int as each is considered as separate entity. So we use both dimensions of the array to access it.

Even if we reduce the size of the array like char juices_A[][12] = { "dragon", "waterberry", "sharonfruit" }; when printed it outputs

dragon;waterberry;sharonfruit

But in the case of int data[3][3] = { { 1, 2}, { 4, 5, 6 }, { 7, 8, 9 } }; it outputs

1,2, 0 ,....` this makes the difference

#include <stdio.h>

int main()
{
    int data[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    int *ptr;
    int lines = 3;
    int columns = 3;
    int i;


    ptr = &data[0][0];

    while (lines-- > 0) {
        i = columns;

        while (i-- > 0) {
            printf("%d ", *ptr);
            ptr += 1;
        }

        putchar('\n');
    }
    return 0;
}

or:

#include <stdio.h>

int main()
{
    int data[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    int *ptr;
    int lines = 3;
    int columns = 3;
    int max = lines * columns;


    ptr = &data[0][0];

    while (max-- > 0) {
        printf("%d ", *ptr);
        ptr++;

        if (max % columns == 0)
            putchar('\n');
    }

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM