通过引用将 char** 传递到函数中

发布于 2024-08-16 04:52:07 字数 983 浏览 4 评论 0原文

季节的问候!我有一个函数可以打印出 char** 的内容,该 char** 用作数组来存储多个字符串。声明如下:

char** commandArray = (char**)malloc(historySize);

其中historySize 是一个全局int,目前设置为8。该数组填充有用户输入的命令,类似于循环队列。我可能希望在多个地方打印出缓冲区的内容,因此我创建了一个函数。理想情况下,该函数接受对 commandArray 的引用并循环遍历它,打印出它包含的内容。现在,我不得不说指针和引用不是我的强项,所以我不确定我做的事情是否正确。该函数如下所示:

    /* prints the contents of the history buffer */
void printHistory(char*** historyBuff)
{
    /* a counter for the loop */
    int loopIdx = 0;

    for (loopIdx = 0; loopIdx < historySize; loopIdx++)
    {
        /* print the current history item */
        printf ("\nhistoryBuff[%i] = %s\n", loopIdx, *historyBuff[loopIdx]);
        fflush(stdout);
    }
}

我将我的 char** 传递到该函数中,如下所示:

printHistory (&commandArray);

就目前而言,一切都编译良好,但是当程序打印历史记录时,该函数挂在循环中的某个位置并且不打印出什么位于 char** 中。所以,我向您提出的问题是:我是否正确传递了 commandArray,我是否正确声明了该函数,以及我是否在函数中以正确的方式取消引用它?

预先感谢您提供的任何帮助或建议!

-本

Season's greetings! I have a function that prints out the contents of a char** that is being used as an array to store a number of strings. The is declared like this:

char** commandArray = (char**)malloc(historySize);

where historySize is a global int set to 8, for now. The array is populated with commands that the user enters, in sort of a circular queue. There are more than a couple places where I may want the contents of the buffer printed out, so I made a function. Ideally, the function takes in a reference to commandArray and loops through it, printing out what it contains. Now, I have to say that pointers and references are not my strong point, so I'm not really sure if I'm doing things correctly. The function looks like this:

    /* prints the contents of the history buffer */
void printHistory(char*** historyBuff)
{
    /* a counter for the loop */
    int loopIdx = 0;

    for (loopIdx = 0; loopIdx < historySize; loopIdx++)
    {
        /* print the current history item */
        printf ("\nhistoryBuff[%i] = %s\n", loopIdx, *historyBuff[loopIdx]);
        fflush(stdout);
    }
}

I'm passing my char** into the function like this:

printHistory (&commandArray);

As it stands now, everything compiles fine, but when the program prints the history, the function hangs somewhere in the loop and does not print out what is in the char**. SO, my question to you is this: Am I passing commandArray properly, am I declaring the function correctly, and am I dereferencing it the right way in the function?

Thank you in advance for any and all help or suggestions!

-Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-08-23 04:52:07
  1. 为了使您的代码按原样工作,您应该像这样取消引用:

    (*historyBuff)[loopIdx]

    您编写的方式,由于 C 中运算符的优先级,[] 发生在 * 之前,这不是您想要的。

  2. 您需要为命令数组分配更多空间。现在它实际上还不够大,无法容纳 historySize char* 的:

    char** commandArray = (char**)malloc(historySize * sizeof(char*));
    
  3. 您不需要通过“引用”传递此数组。您可以像这样声明您的函数:

    void printHistory(char** HistoryBuff)
    

    并直接传入commandArray。如果您打算在函数中的某个位置更改实际的数组指针(例如,如果您需要realloc它以生成更多值),则只需要传入char***空格)。

  4. 对于只打印内容的函数,您可以更进一步并声明内容const。这是对调用者的“保证”(只要您可以保证 C 中的任何内容),您不会修改数组或其中的字符串:

    void printHistory(const char *const * HistoryBuff)
    
  1. To make your code work the way it is, you should dereference like this:

    (*historyBuff)[loopIdx]

    The way you have written things, [] happens before * because of operator precedence in C, and it's not what you want.

  2. You need allocate more space for your command array. Right now it isn't actually big enough to hold historySize char*'s:

    char** commandArray = (char**)malloc(historySize * sizeof(char*));
    
  3. You don't need to pass this array by "reference". You can just declare your function like this:

    void printHistory(char** historyBuff)
    

    And pass in commandArray directly. You would only need to pass in a char*** if you intend to change the actual array pointer somewhere in the function (e.g. if you needed to realloc it to make more space).

  4. For a function that only prints things, you could go a little further and declare things const. This is a "guarantee" to the caller (insofar as you can guarantee anything in C) that you're not going to modify the array or the strings in it:

    void printHistory(const char *const * historyBuff)
    
香草可樂 2024-08-23 04:52:07

1.

malloc 分配一定数量的字节,而不是指针。如果 historySize 是您要分配的字符指针的数量,则需要将:更改

char** commandArray = (char**)malloc(historySize);

为:

char** commandArray = malloc( historySize * sizeof(char*) );

2。

您的 printHistory() 不会更改commandArray 指针。您不需要传递 char***。一个 char** 就可以了。

1.

malloc allocates a number of bytes, not pointers. If historySize is the number of character pointers you are allocating, you will need to change:

char** commandArray = (char**)malloc(historySize);

to:

char** commandArray = malloc( historySize * sizeof(char*) );

2.

Your printHistory() doesn't change the commandArray pointer. You don't need to pass a char***. A char** will do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文