Passing a char pointer
Posted: Sun May 02, 2004 11:13 am
How come when I pass a char pointer to a function and get it to point to a certain place in another char pointer that I passed to that same function, when I return to the calling function, the work that was done in the called function didn't affect my pointer? Do I need to use pointers to pointers?
I've tried it with pointers to pointers but it doesn't work
np = char *nextpipe
bp = bufp
As you can see, my problem is that when I print nextpipe from the caller, it's still NULL and not where I pointed it to in parse_pipes. It doesn't make sense because I modify bufp yet it works properly.
I've tried it with pointers to pointers but it doesn't work
Code: Select all
parse_pipes(bufp, pipes, npipes, nextpipe, &flags)
Code: Select all
int
parse_pipes(char *buf, int (*pipes)[2], int npipes, char *nextpipe, int *flags)
{
if (npipes > 0)
*flags |= P_READ;
if ((nextpipe = strchr(buf, '|')) != NULL) {
*nextpipe++ = 0;
if (pipe(pipes[npipes]) < 0)
return(-1);
*flags |= P_WRITE;
if (strlen(buf) < 1)
return(-1);
}
return(0);
}
Code: Select all
[Aurora:~/dev/c] chris% ./shell
> ls | cd
np: cd
> bp: ls
np: (null)
bp = bufp
As you can see, my problem is that when I print nextpipe from the caller, it's still NULL and not where I pointed it to in parse_pipes. It doesn't make sense because I modify bufp yet it works properly.