0

In C, in a array that looks something like

{{"gsga","baf"},{"ad","aasb","asdf","asdfsd"},{"ads","sd","sd"}}

How to get different types of lengths such as:

  • to get the total size of array (in this example it is 3 because it contains 3 arrays of strings)
  • how to get size of any array within this array (for example size of 2nd array of this array is 4 since the second array contains 4 elements namely "ad","aasb","asdf","asdfsd"
  • how to get size of individual strings of any array (for example the size of 2nd string of 2nd array (i.e. "aasb") is 5).
5
  • 3
    What have you tried/researched? There are answers to all three of your examples on Stack Overflow already. Commented Apr 23, 2016 at 5:46
  • 1
    When dealing with ragged arrays, a common idiom is a sentinel value. Commented Apr 23, 2016 at 5:47
  • 1
    "how to get size of individual strings" You probably know that you can get the length of strings with strlen, which requires the strings to be null-terminated. It is not quite clear what you need your arrays for, but you could terminate your other arrays with NULL, too and implement a length function similar to strlen. (Of course, the terminator would then be a null pointer, not a null char.) Commented Apr 23, 2016 at 5:47
  • 1
    In short you can't get size of an array in C. For size of string, use strlen Commented Apr 23, 2016 at 6:03
  • 1
    Are you operating on the array of strings within the same scope where the array was declared? And is it an array, or are we talking about pointer-to-pointer-to-char?? Commented Apr 23, 2016 at 6:13

2 Answers 2

4

You can get the size of all dimensions (3 x 4 x 7) using the sizeof operator.

If you want to know the length of the last one, use strlen in a loop:

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

int main(void)
{
    char data[][4][7] = {{"gsga","baf"},{"ad","aasb","asdf","asdfsd"},{"ads","sd","sd"}};
    size_t items1 = sizeof data / sizeof data[0]; /* size of dim 1 */
    size_t items2 = sizeof data[0] / sizeof data[0][0]; /* size of dim 2 */
    size_t iter1, iter2, count, len;

    printf("%zu items\n", items1);
    for (iter1 = 0; iter1 < items1; iter1++) {
        count = 0;
        printf("Item %zu\n", iter1);
        for (iter2 = 0; iter2 < items2; iter2++) {
            len = strlen(data[iter1][iter2]); /* size of dim 3 */
            if (len > 0) {
                printf("\tSubitem %zu: %zu characters\n", iter2, len);
                count++;
            }
        }
        printf("\t%zu subitems\n", count);
    }
    return 0;
}

Output:

3 items
Item 0
    Subitem 0: 4 characters
    Subitem 1: 3 characters
    2 subitems
Item 1
    Subitem 0: 2 characters
    Subitem 1: 4 characters
    Subitem 2: 4 characters
    Subitem 3: 6 characters
    4 subitems
Item 2
    Subitem 0: 3 characters
    Subitem 1: 2 characters
    Subitem 2: 2 characters
    3 subitems

data can be also declared as

char *data[][4] = {{"gsga","baf"},{"ad","aasb","asdf","asdfsd"},{"ads","sd","sd"}};

But then you need to check for NULL's to get the number of (non empty) subitems:

    for (iter2 = 0; iter2 < items2; iter2++) {
        if (data[iter1][iter2] != NULL) {
            len = strlen(data[iter1][iter2]);
            ...
        }
    }

Full Code

Sign up to request clarification or add additional context in comments.

2 Comments

No problem ;), but I am curious, how to check the stack with valgrind?
SGCheck: an experimental stack and global array overrun detector: valgrind.org/docs/manual/sg-manual.html
0

The following program prints the values you mention:

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

#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0]))

int main(void)
{
    const char *A[][4] = {{"gsga", "baf"}, {"ad", "aasb", "asdf", "asdfsd"}, {"ads", "sd", "sd"}};

    printf("%d\n", LEN(A)); /*number of rows*/
    printf("%d\n", LEN(A[0])); /*number of columns*/
    printf("%d\n", (int) strlen(A[1][1]) + 1); /*size of element*/
    return 0;
}

8 Comments

Nitpicking: The %d is not nice.
@alk What do you suggest for ANSI C89?
At least an unsigned type. But why C89 at all?
Also the code does not take into account that there might "strings" larger then A[1][1].
@alk Of course there might be strings larger than A[1][1], what was asked for was "the size of individual strings".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.