convert string to pointer to pointer?

Discussion in 'C Programming' started by Joe H, Jan 8, 2004.

  1. Joe H

    Joe H Guest

    Hi i converted a program to a lib. The parameters of main are
    maincommand(int argc, char **argv)
    I want to use main a s function that takes a string how can i convert
    a char * to a char ** or even better how should i substitute the parameters
    for a string. thanks
     
    Joe H, Jan 8, 2004
    #1
    1. Advertising

  2. On Thu, 8 Jan 2004, Joe H wrote:
    >
    > Hi i converted a program to a lib. The parameters of main are
    > maincommand(int argc, char **argv)
    > I want to use main a s function that takes a string how can i convert
    > a char * to a char ** or even better how should i substitute the parameters
    > for a string. thanks


    How many parameters, how organized, etc.? For example, if you're
    used to calling the old program at the command line like this:

    % myprog 100
    % myprog 42
    % myprog -8192

    then you might prototype the new library function as

    int myprog(int arg);

    But if you write big complicated programs that you call like this:

    % myprog -X 310 -Y 100 -bc -g 5
    % myprog -X 80 -Y 80 -b -g 10 -k1 1.5 -k2 .3
    % myprog -c -k2 50

    then you might be better off with a prototype that requires the client
    to explicitly state each possible argument's value:

    int myprog(int X, int Y, int g, double k1, double k2, int B, int C);

    Finally, if you are into clever little parsing algorithms, you
    might write programs that get called like this:

    % myprog "echome"
    % myprog -o foo bar
    % myprog recursive -o foo

    and then maybe it would make sense to let your library function do
    its own splitting-up-of-arguments, and just take a single string
    with all the arguments glommed together, like this:

    int myprog(const char *all_the_arguments);

    It really depends on what you're trying to do.

    If you want to pass a single string to a 'main'-like function
    that takes (int, char **), you can do it like this:

    int callmain(const char *p)
    {
    int my_argc;
    char *my_argv[100];

    my_argc = 2;
    my_argv[0] = "program name goes here, usually";
    my_argv[1] = (char *)p; /* EVIL CAST! EVIL EVIL EVIL! */
    my_argv[2] = NULL;
    return fake_main(my_argc, my_argv);
    }

    Notice the presence of an EVIL EVIL EVIL cast to (char *). That's
    only necessary because I made 'callmain' take a (const char *)
    parameter. I did that only because it seems nicer to the user. It
    works as long as 'fake_main' behaves itself and doesn't go modifying
    'p' behind our backs. Maybe it would be better to remove the 'const'
    altogether... it depends.

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Jan 8, 2004
    #2
    1. Advertising

  3. Joe H

    joe Guest

    thanks for your reponse ill try to make sense out of that
    the program takes the pointer to pointer and the int and gets its
    parameters like this

    while ((ch = getopt(argc, argv,
    "b:c:Dde:f:F:hHk:lmn:eek::pPr:sS:tT:uUv:x:z:")) != -1)

    i would like to give it a string rather than the pointer to pointer. i have
    done this in the main program and it compiles but i cant get the function
    to run

    #include "zib.h"
    int main(int argc, char* argv[])
    {
    char line[100] = "-s -T 1002\n";

    int t = zcommand(1, &line);

    return 1;

    }
     
    joe, Jan 8, 2004
    #3
  4. On Thu, 8 Jan 2004, joe wrote:
    >
    > thanks for your reponse ill try to make sense out of that
    > the program takes the pointer to pointer and the int and gets its
    > parameters like this
    >
    > while ((ch = getopt(argc, argv,
    > "b:c:Dde:f:F:hHk:lmn:eek::pPr:sS:tT:uUv:x:z:")) != -1)


    'getopt' is not a standard C function, and so it's off-topic here.
    That's not just zealous topicality guidance, either -- I really have
    no idea how 'getopt' works, and I don't care. But I can make some
    assumptions, most of them implicit, and let you puzzle out what works
    and what doesn't.

    > i would like to give it a string rather than the pointer to pointer. i have
    > done this in the main program and it compiles but i cant get the function
    > to run


    > int main(int argc, char* argv[])
    > {
    > char line[100] = "-s -T 1002\n";
    >
    > int t = zcommand(1, &line);


    Where 'zcommand' is the old 'main' function, no doubt. Okay, I'm
    not surprised that it didn't work. I recommend that you take a good
    look at the last public C draft standard, N869 (which you can Google
    for; it comes in plain text as n869.txt[.gz]). It has a section
    somewhere detailing exactly what the implementation guarantees to
    be present in 'argc' and 'argv' -- and thus what *you* need to
    guarantee will be present in *your* "fake" argc and argv.
    I would quote N869 right here, but I'm on a slow connection this
    week. Maybe someone else will. Anyway, search the file for the
    words 'argc' and 'argv' and I bet you'll find it.

    You [probably] need to split up those arguments in that string
    literal, like this [but I don't know how lenient 'getopt' might
    be on your platform -- maybe it's a non-issue]:

    char *line[] = {"dummy", "-s", "-T", "1002", NULL};

    int t = zcommand(4, line);

    If this *is* the issue [consult your 'getopt' documentation, such
    as "man getopt" or your compiler manual], then you might need to
    write some code to split up the "command-line arguments" yourself.
    That is, a function that can take the input
    "-s -T 1002\n"
    and produce the output
    {"dummy", "-s", "-T", "1002", NULL}

    If it turns out you do need such a function, check Google for
    "command line argument parsing" or post to a group like
    comp.sources.wanted. (comp.lang.c is *not* the place to ask other
    people to write whole big functions for you.)

    HTH,
    -Arthur
     
    Arthur J. O'Dwyer, Jan 9, 2004
    #4
  5. Joe H

    joe Guest


    >


    thanks alot, that was great
     
    joe, Jan 9, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. overbored
    Replies:
    5
    Views:
    9,200
  2. Neal Becker

    [ctypes] convert pointer to string?

    Neal Becker, May 21, 2008, in forum: Python
    Replies:
    4
    Views:
    3,561
    Neal Becker
    May 21, 2008
  3. asker123
    Replies:
    0
    Views:
    1,336
    asker123
    Jun 27, 2009
  4. Vincent R
    Replies:
    8
    Views:
    547
    Bart van Ingen Schenau
    Jun 30, 2009
  5. Replies:
    2
    Views:
    192
    John Bokma
    Aug 30, 2013
Loading...

Share This Page