Hi to all
Pls, help me in solving the problem execv() function.
My working environment is Solaris and solaris c compiler.
Can any one tell me how to use execv() function in c.
Following is the sample code:
main()
{
char a[][60]={"sample.sh", "one", "two", "three"};
execv("sample.sh", a);
}
but it gives error like "Bad Address"
Pls can any one help me out in this regard.
Thanks in advance
|
Ambitious contributor
|
|
| 26Nov2007,17:23 | #2 |
|
Because a should be an array of pointers, not an array of arrays.
Code:
char *a[] = { "sample.sh", "one", "two", "three", NULL };
|
|
Go4Expert Member
|
|
| 28Nov2007,13:33 | #3 |
|
Thanks a lot Salem...........
Could you pls explain me what is the difference between char [][] and char *[]. |
|
Ambitious contributor
|
|
| 28Nov2007,17:08 | #4 |
|
The first is an array of arrays, the second is an array of pointers.
Compare the results of trying strlen(a[0]) and sizeof(a[0]) for both ways of declaring the array. |
|
Go4Expert Member
|
|
| 29Nov2007,15:57 | #5 |
|
Thanks Salem.
I got your point. But my question is that What is the problem to use the char [][] instead of char *[] in the execv() system call. Thanks in advance.... |
|
Ambitious contributor
|
|
| 29Nov2007,17:17 | #6 |
|
Well it expects an array of pointers, what more is there to understand?
They're not the same thing. Code:
An array of arrays +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |H|E|L|L|O| |W|O|R|L|D| | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |T|H|I|S| |W|O|R|K|S| | | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |W|O|O|!| | | | | | | | | | | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ An array of pointers +----+ +-+-+-+-+-+-+-+-+-+-+-+-+ | |--->|H|E|L|L|O| |W|O|R|L|D| | +----+ +-+-+-+-+-+-+-+-+-+-+-+-+ | |--->|T|H|I|S| |W|O|R|K|S| | +----+ +-+-+-+-+-+-+-+-+-+-+-+ | |--->|W|O|O|!| | +----+ +-+-+-+-+-+ Just because you can do printf("%s", a[i] ); does NOT make them the same thing, because the compiler will generate slightly different code depending on how you declared the array to begin with. |
