通过引用将 char** 传递到函数中
季节的问候!我有一个函数可以打印出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使您的代码按原样工作,您应该像这样取消引用:
(*historyBuff)[loopIdx]
您编写的方式,由于 C 中运算符的优先级,
[]
发生在*
之前,这不是您想要的。您需要为命令数组分配更多空间。现在它实际上还不够大,无法容纳
historySize
char*
的:您不需要通过“引用”传递此数组。您可以像这样声明您的函数:
并直接传入
commandArray
。如果您打算在函数中的某个位置更改实际的数组指针(例如,如果您需要realloc
它以生成更多值),则只需要传入char***
空格)。对于只打印内容的函数,您可以更进一步并声明内容
const
。这是对调用者的“保证”(只要您可以保证 C 中的任何内容),您不会修改数组或其中的字符串: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.You need allocate more space for your command array. Right now it isn't actually big enough to hold
historySize
char*
's:You don't need to pass this array by "reference". You can just declare your function like this:
And pass in
commandArray
directly. You would only need to pass in achar***
if you intend to change the actual array pointer somewhere in the function (e.g. if you needed torealloc
it to make more space).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:1.
malloc
分配一定数量的字节,而不是指针。如果historySize
是您要分配的字符指针的数量,则需要将:更改为:
2。
您的
printHistory()
不会更改commandArray 指针。您不需要传递char***
。一个char**
就可以了。1.
malloc
allocates a number of bytes, not pointers. IfhistorySize
is the number of character pointers you are allocating, you will need to change:to:
2.
Your
printHistory()
doesn't change thecommandArray
pointer. You don't need to pass achar***
. Achar**
will do.