为三重指针分配内存 [英] Allocating memory for triple pointer
本文介绍了为三重指针分配内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
函数somefunction()
以三重指针作为参数.
The function somefunction()
takes a triple pointer as an argument.
int somefunction(tchar ***returnErrors);
如何为returnErrors
参数分配内存?
推荐答案
正在猜测. .
您可以将returnErrors视为指向字符串数组的指针.
You can think of returnErrors as a pointer to an array of strings.
- 第一个*实现指向数组的指针 tchar(或单个字符串) tchars)
- 第二个*实现指向 字符串数组.
- 最后一个*是,因此您可以更改 returnErrors并传回新的 记忆.
- The first * imples pointer to array of tchar (or a single string of tchars)
- The second * imples a pointer to an array of strings.
- The last * is so you can change returnErrors and pass back the new memory.
为此目的分配内存(愚蠢的示例,在SomeFunction内部分配内存)
To delare memory for this (silly example, allocating memory inside SomeFunction)
tchar ** errors;
// Oops it appears I need to pass back two error strings (+ 1 for null on end, so we know there are no more - thanks tlholaday)
errors = malloc(sizeof(tchar*) * 3);
// the first string has length 20 (+ 1 for null terminator)
errors[0] = malloc(sizeof(tchar) * 21);
// the second string has length 30 (+ 1 for null terminator)
errors[1] = malloc(sizeof(tchar) * 31);
// ensure the last is null
errors[2] = 0;
*returnErrors = errors;
注意:调用函数需要知道SomeFunction已分配了内存并需要释放它.
这篇关于为三重指针分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文