0

Possible Duplicate:
How do I use arrays in C++?

I have the following C code:

int main () {
    char *pathvc[MAX_PATHS];
    parsePath(pathvc);

    struct command_t command;

    command.name = lookup(command.argv, pathvc); //command.argv is: char *argv[MAX_ARGS];
}

char* lookupPath(char **argv, char **dir) {
    //Some implementation
}

This is the debug information that I am getting in the main function:

Name : pathvc Details:{0x804c208 "/usr/lib/lightdm/lightdm", 0x804c221 "/usr/local/sbin", 0x804c231 "/usr/local/bin", 0x804c240 "/usr/sbin", 0x804c24a "/usr/bin", 0x804c253 "/sbin", 0x804c259 "/bin", 0x804c25e "/usr/games", 0x0 } Default:0xbffff0a8 Decimal:3221221544 Hex:0xbffff0a8 Binary:10111111111111111111000010101000 Octal:027777770250

Name : pathvc[0] Details:0x804c208 "/usr/lib/lightdm/lightdm"

Name : pathvc[1] Details:0x804c221 "/usr/local/sbin"

Name : pathvc[2] Details:0x804c231 "/usr/local/bin"

Name : pathvc[3] Details:0x804c240 "/usr/sbin"

Name : pathvc[4] Details:0x804c24a "/usr/bin"

Name : pathvc[5] Details:0x804c253 "/sbin"

Name : pathvc[6] Details:0x804c259 "/bin"

Name : pathvc[7] Details:0x804c25e "/usr/games"

However when I pass pathvc in the lookup function, I am getting this:

Name : dir Details:0xbffff0a8 Default:0xbffff0a8 Decimal:3221221544 Hex:0xbffff0a8 Binary:10111111111111111111000010101000 Octal:027777770250

Name : *dir Details:0x804c208 "/usr/lib/lightdm/lightdm"

Name : **dir Details:47 '/'

Why is it that I am passing an array of pointers to the lookup function which accepts it as a double pointer but it is only pointing to the first path instead of complete list of paths.

Pardon me if I am doing/asking a very silly question because I am not comfortable with pointers in C.

Community
  • 1
  • 1
Rafay
  • 6,108
  • 11
  • 51
  • 71
  • `command.argv` appears to be unitialised. This will be undefined behaviour if `lookup()` reads from the pointer array. – hmjd Oct 08 '12 at 14:26
  • @hmjd we are not even concerned with command.argv here. And btw, code is properly implemented and the one given here is only a prototype of what is being done. – Rafay Oct 08 '12 at 14:33
  • 1
    @Spoilt, ok. I can only comment on the posted code. I thought there was a mismatch in the content of the same data structure. Dereferencing uninitialised pointers is undefined behaviour. I mentioned this as it may have been a contributing factor. – hmjd Oct 08 '12 at 14:36

2 Answers2

2

It's just a matter of what the debugger knows. In the caller, the debugger can see that there are MAX_PATHS pointers. In the called function, it has no idea, so it doesn't know how many entries to show you. If you manually asked it for the others, it would show them to you.

It's just like this:

void someFunc(int *j)
{
   // does j point to a single integer? An array? Who knows?
}

void foo(void)
{
   int p = 3;
   int* j = &p;
   // here we know j points to one integer
   someFunc(j);
}

void foo2(void)
{
   int* j[4];
   // here we know j is an array of four pointers
   someFunc(j);
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278
1

*dir is the same as pathvc[0], it is only the first element of the array. **dir is not pathvc[1] it is pathvc[0][0]. Use *(dir + 1) or dir[1] instead.

Eregrith
  • 4,263
  • 18
  • 39