execv in unix prob.

Discussion in 'C' started by saravanan_, Nov 26, 2007.

  1. saravanan_

    saravanan_ New Member

    Joined:
    Oct 24, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Because a should be an array of pointers, not an array of arrays.
    Code:
    char *a[] = { "sample.sh", "one", "two", "three", NULL };
    
     
  3. saravanan_

    saravanan_ New Member

    Joined:
    Oct 24, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Thanks a lot Salem...........
    Could you pls explain me what is the difference between char [][] and char *[].
     
  4. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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.
     
  5. saravanan_

    saravanan_ New Member

    Joined:
    Oct 24, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    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....
     
  6. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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|!| |
    +----+    +-+-+-+-+-+
    
    If you pass an array of arrays to something expecting an array of pointers, it's just going to take the first 4 characters of your string (say), and try and use that as a pointer to some other string. Then you immediately get something like unable to reference memory "0x41424344" where all the bytes of the "address" look remarkably like printable characters (in this case, ABCD).

    Just because you can do
    printf("%s", a );
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice