Thread: String to argv[] conversion

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    21

    String to argv[] conversion

    Good morning,

    Is there a simple way of converting a string (char *s) to an array of the type argv[] (console mode)? I'm trying to convert a console program I made to a dll.

    Kind regards,

    JKepler

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    Like this?
    Code:
    char *s1 = "hello";
    char s2[] = "world";
    char *myArgv[] = {
      s1,
      s2,
      NULL
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    21
    Hi

    I was thinking something like this:

    Code:
    char *string =  "Mark,John,Mathew";
    char *argv[10];
    int i=0;
    
    argv[i] = strtok(string,",");
    
    while(argv[i]!=NULL)
    {
       argv[++i] = strtok(NULL,",");
    }
    But I'm not sure if I'm thinking straight...
    Can the string be *string or must it be string[]?

    JKepler

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,675
    string must be an array if you intend to chop it up with strtok().
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv to string segmentation fault
    By tkks in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2013, 01:13 PM
  2. Argv to string?
    By azrael in forum C Programming
    Replies: 8
    Last Post: 03-12-2009, 11:37 AM
  3. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  4. Is argv[] terminated by a NULL string?
    By dwks in forum C Programming
    Replies: 9
    Last Post: 07-24-2005, 10:24 AM
  5. int main (int argc, string& argv[]) ??
    By cboard_member in forum C++ Programming
    Replies: 17
    Last Post: 07-16-2005, 03:38 AM

Tags for this Thread