Thread: How do i make an array of pointers for execvp

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    34

    How do i make an array of pointers for execvp

    I am trying to use the execvp command and require an array of pointers for the 2nd argument.
    I am storing the arguments in an array of strings. How would I turn that array of strings into an array of pointers?

    Also, how can I terminate the array of pointers with a null pointer.

    I was using the array of strings in the execvp 2nd argument but that was throwing errors and I was trying to make the last entry of the array a null pointer, using strcat and strcpy but both didn't work.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    type **array_of_pointers;
    array_of_pointers = malloc(NR_POINTERS * sizeof(type*));
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The first hit for exec: Splitting a space delimited string

    But that's assuming you know how to click search and type a word, and click another button or two. I know, I know, it's asking a lot of you.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    34
    Sorry, I forgot to use the search function here. Was just googling.

    Some questions about your code for storing from that thread.

    Code:
    char **args = NULL;
    int argcount = 0;
    
    while input
        pop a word off into a buffer
        char **temp;
        temp = realloc( args, 1 + argcount * sizeof *temp )
        if( temp )
        {
            args = temp;
            args[ argcount ] = malloc strlen buf + 1 
            if( args[ argcount ] )
                strcpy( args[ argcount ], buffer )
            argcount++;
        }
    realloc again, adding room for a NULL ptr, or do it at the start, before the loop
    Do you need a (char *) before realloc? I couldn't really understand this line from the man page on realloc.
    The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
    Also, how would you add a NULL ptr in?

    Strcpy does not let you put NULL as an argument.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just assign the last pointer NULL:
    Code:
    args[ lastspot ] = NULL;

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  2. MergeSort with array of pointers
    By lionheart in forum C Programming
    Replies: 18
    Last Post: 08-01-2008, 10:23 AM
  3. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array of pointers to structs
    By stumon in forum C Programming
    Replies: 7
    Last Post: 03-24-2003, 07:13 AM