Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

c - Array of arrays, with different sizes

I'm having an array, that has arrays in every cell. For example, the big array is called arr:

int a[3] = {3, 2, 1};
int b[2] = {2, 1};
int *arr[2] = {a, b}

Now the problem is, if I want to print the small arrs, inside the big array.

Here is my code:

#include <stdio.h>

void printArr(int arr [], int n)
{
    for (int i = 0 ; i < n ; i++)
    {
        printf("%d ", *(arr + i));
    }
    printf("
");
}

int main()
{
    int a[5] = {1, 8, 4, 2, 0};
    int b[3] = {1, 4, 2};
    int *arr [2] = {a, b};

    int n = 0;

    for (int i = 0 ; i < 2 ; i++)
    {
        printArr(*(arr + i), n);
    }
}

The output is supposed to be something like this:

1 8 4 2 0 1 4 2

But I can't get the size of each array, since sizeof(*(arr + i) gives me 4, which is the size of the pointer (the name of the array), and not all the array it self. So what can I do?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Problem:

The C language only provides a way of finding the size of types. This gives the subtle differences between applying sizeof to:

1) An array of a type such as:

int a[3];
sizeof(a); // => 3 * sizeof(int)

2) A pointer to the type:

int *ptr;
sizeof(ptr); // => sizeof(int *)

or

int a[3] = {3, 2, 1};
int b[2] = {2, 1};
int *arr[2] = {a, b};

sizeof(arr[1]); // => sizeof(int *)

Some solutions:

Store the size

As jfly proposes store the size of the arrays.

  • Makes finding the size a constant time operation.

Append an end marker

Adding a end marker like '' as used for c-style strings. You might use INT_MAX or INT_MIN in this case.

The printArr implementation would need to change to:

void printArr(int *arr)
{
    int *it = arr;
    while(arr != INT_MIN);
    {
        printf("%d ", *it);
    }
    printf("
");
}

Disadvantages:

  • Finding the size of the array requires iterating over the full array.
  • Gives the risk of an actual value colliding with the end marker value.

Advantages:

  • The varying sized array can be passed as a single argument.

Using iterators

Store the pointer to the first and one past the last value.

void printArr(int *begin, int *end)
{
    for (int *it = begin; it != end; it++)
    {
        printf("%d ", *it);
    }
    printf("
");
}

int *end_arr[2] = {a + 3, b + 2};

for (int i = 0 ; i < 2 ; i++)
{
    printArr(arr[i], end_arr[i]);
}
  • Can be extended to other data structures.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...