从函数改变值 [英] changing value from function

查看:122
本文介绍了从函数改变值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改函数中的2dim字符数组。

I want to change a 2dim char array in a function.

我分配像

char **u;
u = new char * [ MAX_DEPTH ];
for (i=0; i<MAX_DEPTH; i++)
    u[ i ] = new char [ BUFFER_SIZE ];

该功能看起来像

rem(char ***arr, int max_length, char *url)
{
    int idx=0;
    char * p;
    int i;

    p = strtok (url,"/"); 

    while (p != NULL && idx < max_length)
    {

        for (  i=0; i<maxUrlSize-1 && p[i] != '\0'; i++)
            (*arr)[idx][i] = p[i];
        for (     ; i< maxUrlSize-1; i++)
            (*arr)[idx][i] = '\0';
    }
}

该函数将在我的主程序中使用。 / p>

the function will be used in my main program.

rem( &u, MAX_LEN, url);

但是离开函数后没有任何内容。有人能解释我如何使用指针?

but after leaving the function there is nothing in. Could someone explain me how to use pointers in this way?

推荐答案

您需要在函数中更改引用 tmp ,到 arr 。您不是正在访问参数 arr 。此外,您不需要 char *** 在这里,因为您不改变分配给 u 的空间。相反,你应该有参数 char ** arr ,它作为 arr [i] [j] 访问。然后,您应该将 u 传递给 rem ,而不是& u

You need to change the reference to tmp in your function, to arr. You aren't accessing the parameter arr at all. Also, you do not need char *** here, since you aren't changing the space allocated to u. Instead, you should have parameter char **arr, which you access as arr[i][j]. And you should then pass u to rem, rather than &u.

这篇关于从函数改变值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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