0

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

I allocate the space like

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

the function looks like

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';
    }
}

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?

Taryn
  • 242,637
  • 56
  • 362
  • 405
Roby
  • 2,011
  • 4
  • 28
  • 55
  • There;s nothing obviously wrong, but more than 1-line fragments would really help - we have to infer that the function implementation is rem(char ***tmp, int ?) for the code to even begin to make sense. – Phil Lello May 02 '11 at 04:35
  • The code you posted is all correct. – Borealid May 02 '11 at 04:38
  • @Roby: Please edit and align your code. One of the members took the pains for doing it for you the first time,You should learn it from there...Can't have a endless loop of your edits & then counter edits to make it readable. – Alok Save May 02 '11 at 04:45
  • Without seeing the actual code in `rem`, what you have looks OK. However, unless you are reallocating the space assigned to `u` inside the function `rem`, you don't really need to pass `&u` here. Just pass `u` instead. The receiving parameter `tmp` should just be `char **`, and you would access it as `tmp[i][j]`. EDIT to comment: What is `tmp`? Looks like a global. – Joel Lee May 02 '11 at 04:45
  • @Als: i will try it :P new on this site :) thx – Roby May 02 '11 at 04:48
  • Joe: im sorry tmp is array i removed some lines of code because there arent important to post... – Roby May 02 '11 at 04:51
  • @Roby: Sure :) Welcome and wish you happy learning. – Alok Save May 02 '11 at 04:52
  • From the function, this code looks absolutely fine. Could u please provide the code on how you are invoking this method from other parts of the program. – Purnima May 02 '11 at 04:43
  • @Roby: why not pass the `u` as `rem (u, MAX_LEN, url)` , and simplify the extra pointer `***`` in yout `rem` function? – phoxis May 11 '11 at 14:36

1 Answers1

1

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.

Joel Lee
  • 3,656
  • 1
  • 18
  • 21
  • you are right, i removed some lines of code and paste it together... tmp is arr ... im sorry used the wrong name :-/ – Roby May 02 '11 at 04:53