k t
k t

Reputation: 363

how to get the string-lengths stored in array (C)

  1. How would i get the lengths of the belowed strings, as well as the array size?

    char str [] = {};

        str[0] = "He";
        str[1] = "llo";
        str[2] =  " Wor";
        str[3] ="ld";
    

  2. And how i could store them in a multidimension array? So this array would look something like this:

    char strstr [size of str][string length of str];

The strstr[][] array-dimensions should be
1.number of array-elements of str and
2.number of all chars in str. In the case of strstr[3][1], this would be "d".
Would that be possible without initializing the str-array in the first place?


[edit] I see, the second dimension of strstr doesn't make sense. It should be the length of every element from *str and not the complete number of chars in *str [/edit]

Upvotes: 2

Views: 2184

Answers (4)

jkerian
jkerian

Reputation: 17016

This will probably do what you want, but it wasn't exactly what you asked for.

#include <stdio.h>

char *strs[] = {
    "foo",
    "bar",
    "bazy"
};

int main() {
    printf("%d - %s\n", strlen(strs[0]), strs[0]);
    printf("%d - %s\n", strlen(strs[1]), strs[1]);
    printf("%d - %s\n", strlen(strs[2]), strs[2]);
    return 0;
}

Output:

3 - foo
3 - bar
4 - bazy

Note that you only have a few possibilities for storing arrays of arrays. You can either do what this solution does (make an array of pointers to arrays), make a "compressed" list of arrays in a large array, or make an overly large 2-D array.


The "compressed" array would take the format:

char strs[] = "foo\0bar\0bazy"; // array is {f,o,o,\0,b,a,r,\0,b,a,z,y,\0}

The problem with this format is that it's somewhat tricky to access anything after the first element, and usually involves searching linearly through the array. (or keeping a seperate table of addresses... rather like the first solution)


The 2-D array requires that you specify all sizes, and looks like this:

char strs[3][5] = {
   "foo",
   "baz",
   "baxy"
};

int main() {
    printf("%d - %s\n", strlen(strs[0]), strs[0]);
    printf("%d - %s\n", strlen(strs[1]), strs[1]);
    printf("%d - %s\n", strlen(strs[2]), strs[2]);
    return 0;
}

This is probably laid out in memory like this:

{f,o,o,\0,*,b,a,z,\0,*,b,a,x,y,\0} (the *'s could be anything, most dependent on your compiler)

Since the compiler knows that each string takes exactly 5 bytes, it can easily calculate the location of the later elements.

Upvotes: 5

drewmm
drewmm

Reputation: 737

When declaring an array in C, you must declare its size unless you are simultaneously providing initializing values. This is because arrays are laid out sequentially in memory, and so the computer needs to know how much space to set aside.

Further, char str [] = {....} will declare an array of characters, not an array of strings. A string is an array of characters, so str is just a single string.

To create an array of strings, you need to do something like this:

char strs[num_of_strings][max_length_of_strings+1];
strcpy(strs[0],"string1");
strcpy(strs[1],"string2");
...

There's a more in depth discussion of other options at this question.

If you're specifying max_length_of_strings but then copying from source strings that you can't guarantee are shorter than max_length_of_strings, you want to use strncpy instead of strcpy. In that case you also want to ensure that the resulting strings are null terminated, which you can do by looping

strs[i][max_length_of_strings] = '\0';

To get the length of a null-terminated string, you use the function strlen.

As to the second part of your question, the above should make it clear how to create a higher-dimensional array. I'd provide more detail, but I'm not exactly sure what you want this array to do. When you say "number of all chars in str" do you mean the max length of the strings stored in your strs array?

If so, you don't need to worry about that. Programming your strs array like I explained it above will already give you this functionality: since strings are just arrays of char's, you can index into strings in the same way that you index into char's.

Let me know if any of the above isn't clear, and I'll try to explain it better.

Upvotes: 2

teppic
teppic

Reputation: 8195

If you want an actual array, something like this:

    char str[4][5];  // holds up to 4 characters in each
    strcpy(str[0], "He");
    strcpy(str[1], "llo");
    strcpy(str[2], " Wor");
    strcpy(str[3], "ld");

Upvotes: 1

Tony The Lion
Tony The Lion

Reputation: 63190

To get the length of a string in C you'd use strlen and pass the string.

int len = strlen(array[0]);

Upvotes: 0

Related Questions