【问题标题】:C - what does while(*Table[i]) mean/do?C - while(*Table[i]) 是什么意思/做什么?
【发布时间】:2016-01-22 09:09:21
【问题描述】:

我需要向数组中添加一个字符串,并且需要一些帮助来弄清楚这意味着什么。这是我所拥有的:

#include <stdio.h>
#include <stdlib.h>
int insert(char *word, char *Table[], int n)
{
//*word is the string to be added, *Table[] is the array, n is
//the return value, which is the number of words in the array after adding *word
    int i = 0;
    while(*Table[i])
    {
        if strcmp(*Table[i], *word) == 0)
        {
            return n;
            break;
        }
    }
}

我前段时间写了这篇文章,现在正在重新审视它。我不知道 while *Table[i] 是什么意思,因此我不知道后面的代码是什么意思。另外,这段代码不完整,所以不要告诉我它不会添加字符串。

【问题讨论】:

    标签: c arrays string while-loop


    【解决方案1】:

    * 运算符取消引用指针,[i] 也是如此。

    由于 Table 被声明为 char *Table[],这与 char ** 相同,因为它是指向指针类型的指针(如二维数组)。

    在这种情况下,从用法可以明显看出 Table 是一个字符串数组(字符串是一个 char 数组(因此是数组类型的数组))。

    所以 Table[i] 是指向字符串数组中第 i 个字符串的指针,然后 * 再次取消引用它。作者这里做的就是在字符串数组后面找一个NULL(零),这明明就是判断数组结尾的方法。

    【讨论】:

      猜你喜欢
      • 2014-01-12
      • 2014-04-06
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多