通过引用传递指针 [英] pass pointer by reference

查看:72
本文介绍了通过引用传递指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,


我想编写一个初始化字符串函数来mallocs一个区域,用一个字符填充

,将NUL字符添加到最后一个位置并将其返回给

用户。


到目前为止,我注意到我的实现始终适用于新内存

区域,而不是ORIGINAL指针区域,所以初始字符串

区域未被触及,指针未更新以引用新的

发起区域...


发生了什么事?请参阅下面的代码...


int main(){

char * string;

initstring(string,''' \0'',14); / *创建一个字符串,其中包含NUL

和15位额外的NUL * /

/ *如果我在这里检查*字符串,则从第1个语句开始就不受影响< br $>
* /

}

void initstring(char * array,char c,unsigned int size){

array = malloc((size + 1)* sizeof(char));

int i;


for(i = 0; i< size; i ++){

/ * array [i] = c;数组索引* /

*数组= c;

数组++;

}

/ *数组[size + 1] =''\''';数组索引* /

* array =''\ 0'';

}


所以,为什么*阵列"总是进入函数initstring与

不同的值(内存地址)比initistring()函数调用
(相应)?

我的环境:Eclipse + CDT + Cygwin(Win32)

解决方案

HSeganfredo写道:


[...]


int main(){

char * string;

initstring(string,''\'',14); / *创建一个字符串,其中包含NUL

和15位额外的NUL * /

/ *如果我在这里检查*字符串,则从第1个语句开始就不受影响< br b> b $ b $ /
>
void initstring(char * array,char c,unsigned int size){

array = malloc((size + 1)* sizeof(char));



你将指针的* copy *传递给initstring函数。

修改initstring中的数组不会影响字符串变量

in main。


如果要修改指针,必须将指针传递给指针。


initstring(& string,''\'',14);

/ * ... * /

void initstring(char ** parray ,char c,unsigned int size){

char * array;

array = * parray = malloc((size + 1)* sizeof(char));


BTW,你应该尊重C程序员的第六条诫命:

"

如果宣布一个函数返回错误代码在遇到困难的情况下,你要检查那段代码,是的,即使支票是你的代码大小的三倍,并且在你的打字手指上产生疼痛,对于

,如果你说它不会发生在我身上,那么众神肯定会惩罚你的b
你的傲慢。

"


你必须检查malloc()的返回值!


-

您可以通过< ta ***************** @ yahoDELETETHATo.fr>

与我联系


initstring(string,''\'',14);



正如有人已经说过的那样,你必须传递

指针的地址,即指向指针的指针。


实际上,我只是从函数返回一个指针:


未经检查,未经测试的代码。 。 。


char * const initstring(char const c,size_t const len)

{

char * const retval = malloc( len + 1);


char * p = retval;

char const * const pnull = retval + len;


while(p!= pnull)* p ++ = c;


* p = 0;


返回retval;

}

我使用返回值中的const作为程序员

的指示符,他们可能想要挂起该值。


Martin


感谢指针,在这里完美地工作。


我开始记得指针引用func(& ptr)在

调用函数中总是暗示原型中的双指针

(** ptr),一个包含原始指针引用(对于我的char中的char)
case)和另一个包含对该指针的引用。这是一个有点复杂的主题,这使我多年没有处理。谢谢。


Folks,

I want to write a init string function that mallocs an area, fills it
with a char, sticks a NUL char in the last position and returns it to
the user.

So far I noticed that my implementation always works over a NEW memory
area, and not over the ORIGINAL pointer area, so the initial string
area is untouched, the pointer is not update to refer to the new
initiated area...

What is going on? See my code below...

int main(){
char *string;
initstring(string, ''\0'', 14); /* create a string filled with NUL
and a extra NUL at position 15 */
/* if I check *string here, it is untouched since the 1st statement
*/
}

void initstring(char *array, char c, unsigned int size){
array = malloc((size + 1)* sizeof(char));
int i;

for(i=0;i<size;i++){
/* array[i] = c; array indexing */
*array = c;
array++;
}
/* array[size+1] = ''\0''; array indexing */
*array = ''\0'';
}

So, why "*array" always get into the function initstring with a
diferent value (memory address) than when the initistring() function
is called (according)?

My environment: Eclipse + CDT + Cygwin (Win32)

解决方案

HSeganfredo wrote:

[...]

int main(){
char *string;
initstring(string, ''\0'', 14); /* create a string filled with NUL
and a extra NUL at position 15 */
/* if I check *string here, it is untouched since the 1st statement
*/
}

[...]

void initstring(char *array, char c, unsigned int size){
array = malloc((size + 1)* sizeof(char));

You pass a *copy* of the pointer to the initstring function.
The modification of array in initstring won''t affect the string variable
in main.

You must pass a pointer to pointer, if you want to modify the pointer.

initstring(&string, ''\0'', 14);
/* ... */
void initstring(char **parray, char c, unsigned int size){
char* array;
array=*parray = malloc((size + 1)* sizeof(char));

BTW, you should respect the sixth commandment for the C programmer:
"
If a function be advertised to return an error code in the event of
difficulties, thou shalt check for that code, yea, even though the checks
triple the size of thy code and produce aches in thy typing fingers, for
if thou thinkest ``it cannot happen to me'''', the gods shall surely punish
thee for thy arrogance.
"

You must check the return value of malloc()!

--
You can contact me at <ta*****************@yahoDELETETHATo.fr>


initstring(string, ''\0'', 14);


As someone has already said, you have to pass the address of the
pointer, i.e. a pointer to a pointer.

Really though, I''d just return a pointer from the function:

Unchecked, untested code. . .

char *const initstring(char const c,size_t const len)
{
char *const retval = malloc(len + 1);

char *p = retval;
char const *const pnull = retval + len;

while (p != pnull) *p++ = c;

*p = 0;

return retval;
}
I use the const in the return value as an indicator to the programmer
that they might want to hang on to the value.

Martin


Thanks for the "pointers", worked here perfectly.

I started to remember that a pointer reference "func(&ptr)" in the
calling function always implies on a double pointer in the prototype
(**ptr), one contains the original pointer reference (to char in my
case) and another one containing the reference to that pointer. It′s a
somewhat complex subject that makes years I don′t deal with. Thanks.


这篇关于通过引用传递指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆