简体   繁体   English

如何通过char **指针进行迭代?

[英]How to iterate through a char ** pointer?

I have the following code 我有以下代码

struct my_struct {
    const char **enjoy;
};

const char * enjy[] = {
    "Cricket", "movie", "",
    "Ball", "eat", "",
    };

static const struct my_struct my_struct_table[1] = {
    [0] = {
        .enjoy = enjy
        }
};

Now I want to use that final structure and want to iterate using that. 现在,我想使用该最终结构,并要使用它进行迭代。 How can I iterate using my_struct_table[0].enjoy 如何使用my_struct_table[0].enjoy进行迭代my_struct_table[0].enjoy

I want to print all the strings in the enjy variable. 我想打印enjy变量中的所有字符串。

Let T be any type. T为任何类型。 When working on an array of T of varying size, represented as T* , you need to specify how the end of such array is represented. 在处理大小不同的T数组(表示为T* ,您需要指定如何表示此数组的结尾。

In a simpler case: for a string of characters, ie T = char , the end of array char* is typically represented by a null character \\0 . 在一个更简单的情况下:对于字符串,即T = char ,数组char*的结尾通常由空字符\\0 Thus, you can iterate it as: 因此,您可以将其迭代为:

char* ptr = myString;
for (char c = *ptr; c; c=*++ptr) {
    ...
}

You iterate over all characters, until you reach the one that is \\0 , making the expression c evaluate to false / 0 and break the loop. 您遍历所有字符,直到到达\\0为止,使表达式c计算结果为false / 0并中断循环。

An alternative representation for a string is to represent the length of the string as a separate number. 字符串的另一种表示形式是将字符串的长度表示为单独的数字。 This is done, for example, in Pascal strings. 例如,这是在Pascal字符串中完成的。

int size = myStringSize;
for (int idx=0; idx<size; ++idx) {
    char c = myString[idx];
}

Either of the approaches can also be used when you have an array of strings (ie T = char* ). 当您有一个字符串数组(即T = char* )时,也可以使用这两种方法。 Your options are: 您的选择是:

  • You store a special non-string value in your enjoy array set to NULL at the end of the array 您将一个特殊的非字符串值存储在您enjoy数组中,并在数组末尾将其设置为NULL
  • Or you store the total size of the enjoy array in a separate value. 或者,您可以将单独的值存储enjoy数组的总大小。

You can also use both options -- this is the case, for example, with arguments given to int main(int argc, char** argv) . 您还可以同时使用这两个选项,例如,对于int main(int argc, char** argv)给出了参数。 The argc stores the number of string values in the argv , and argv[argc] is guaranteed to be NULL . argc将字符串值的数量存储在argv并且 argv[argc]确保为NULL

If you use the first option you would then iterate it as: 如果使用第一个选项,则将其迭代为:

char** ptr = enjoy;
for (char* c = *ptr; c; c=*++ptr) {
    ...
}

and if you use the second option: 如果您使用第二个选项:

int size = enjoySize;
for (int idx=0; idx<size; ++idx) {
    char* str = enjoy[idx];
}

Notice the similarity of these snippets iterating over char** , to those used for iterating over a simple char* . 请注意,这些片段在char**进行迭代与在简单char*进行迭代所使用的相似。

Note that a value NULL stored in the enjoy array is different than storing a pointer to an empty string. 请注意,存储在enjoy数组中的值NULL与存储指向空字符串的指针不同 The latter should not be used as a marker for the end of the array, because it can lead to hard-to-track bugs when a legitimate empty-string value is added to your enjoy array. 后者不应用作数组末尾的标记,因为当在您的enjoy数组中添加合法的空字符串值时,它可能导致难以跟踪的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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