Menu

Converting an array to a double char

nvellios
2007-10-27
2012-09-26
  • nvellios

    nvellios - 2007-10-27

    This may be the wrong forum to ask this, and if it is I apologize. I know this is a simple question and with a forum full of programmers I should be able to get this answered anyways.

    I have the following

    char y[x];

    I want use 'test' in a function that used to be a main function for a command line program I wrote a while ago.

    int main(int argc, char **argv);

    I turned main into testtest.

    int testtest(int argc, char **argv);

    and want to use the variable 'y' as the 2nd argument in testest so I can use it as argv.

    Any help would be appreciated. I setup a program using Win32 API and have a text window that I want to be able to type text into and run that text into the testtest function and use the code from my old program without much editing of my old program.

    Thanks

    -Nick

     
    • nvellios

      nvellios - 2007-10-27

      This is actually what I have so far with what you gave me. It compiles but crashes.
      I am not sure if that is even the correct code for getting the text from a window. From digging through some of the examples I have it looks like it should work, but it crashes.

      If I force the arguments and add them one at a time to argv it compiles and runs fine if I run it through the testtest function, so I know it has to be something in this code here.

      char *py;
      int argc = 0;
      char
      argv[256];
      DWORD dwTextLength;

      dwTextLength = GetWindowTextLength(GetDlgItem(hwnd, IDC_CHILD_EDIT));
      argv[0] = "testtest";
      argv[1] = strtok( LPSTR(GlobalAlloc(GPTR, dwTextLength + 1)) , "\t " );
      while( argc < 256 && argv[argc] != 0 )
      {
      argc++ ;
      argv[argc] = strtok( 0, "\t " );
      }
      argc--;
      testtest( argc, argv );

      I really appreciate your help guys. You have no idea. :) Thanks!

      -Nick

       
    • Soma

      Soma - 2007-10-27

      "char argv[2][256] ;"

      "argv[0] = "testtest" ;"
      "argv[1] = y ;"
      "testtest( 2, argv ) ;"

      O_o

      No. No! NO!

      What do you think you are doing?

      Re: Nick

      You haven't actually put anything into the memory. There is nothing to parse.

      Soma

       
      • Anonymous

        Anonymous - 2007-10-28

        Gosh.... did I type that!? What was I thinking?

         
    • Osito

      Osito - 2007-10-27

      I'm sure there's a better way, but I think this would work (assuming y[x] is a single string, which doesn't quite match **argv which is more like an array of strings):

      char y[10] = "Whatever";
      char *py; // pointer to y
      py=&y[0];
      return_value = testtest(some_int,&py);

      Then within testtest you can access your original string y using argv[0]:

      cout << argv[0] << endl;

       
    • nvellios

      nvellios - 2007-10-27

      Hmm, still doesn't work. This is damn frustrating...It compiles now, but just plain isn't working.

       
    • Anonymous

      Anonymous - 2007-10-27

      The first element of an argv array is always the command executable path/name, so you need ot dummy that to get the argument y in the right location. You cannot pass any only some_int - it has to be teh number of arguments (including the path/name).

      char argv[2][256] ;
      argv[0] = "testtest" ;
      argv[1] = y ;
      testtest( 2, argv ) ;

      The array y MUST be a null terminated string.

      Now if this continues to not work, you will have to post the code.

       
      • Osito

        Osito - 2007-10-27

        Actually in my example you can pass an arbitrary some_int because his array didn't have two dimensions. He just had y[x] not y[x][x] so argc is no longer relevant. But if his testtest function was actually going to use argc and argv as they were originally intended as part of main, then yes you're right and the OP's definition of y was wrong. I was trying to make it work with his definition of y.

         
        • Anonymous

          Anonymous - 2007-10-27

          testetest() was a rename of main() so it seemed reasonable that he wanted it to work unchanged.

           
    • nvellios

      nvellios - 2007-10-27

      andybuonviri, yes I am using char y[x]; because that when using some example code for the Win32 API that is what is used to get the text from a text window. I want to try and do this without too much code editing so I figure the easiest way to do it would be to convert it to work in a function requiring char **argv

       
      • Anonymous

        Anonymous - 2007-10-27

        Does y contain only one argument, or a string of space separated arguments? If the latter you need:

        int argc = 0 ;
        argv*[256] ;

        argv[0] = "testtest" ;
        argv[1] = strtok( y, "\t " ) ;
        while( argc < 256 && argv[argc] != 0 )
        {
        argc++ ;
        argv[argc] = strtok( 0, "\t " ) ;
        }
        argc-- ;
        testtest( argc, argv ) ;

        Clifford

         
      • Osito

        Osito - 2007-10-27

        Clifford's scheme should work for you. It's way better than what I suggested since it doesn't require you to change any code within testtest(). If it were me, I'd probably clean up testtest to accept the single string rather than argc/argv, but that's up to you.

         
    • nvellios

      nvellios - 2007-10-27

      Alright. I got it working. :) Thanks guys!

      It was working all along but some other code from the example was causing problems the whole time.

      Alright, so now the only thing I got to do is parse the commands and I am good to go. ;)

       
    • nvellios

      nvellios - 2007-10-27

      slocombe, your code makes it crash when I use it. Not sure why.

       
      • Osito

        Osito - 2007-10-28

        I think it has a couple of typos:

        "argv[256];" should probably be "char argv[256];"

        argv[0]="testtest"; is not quite right. It should be something like: char temp[]={"testtest"}; argv[0]=&temp[0];

        argv is an array of pointers so it can't actually store the string "testtest", just the pointer to the array that does store it.

         
      • Anonymous

        Anonymous - 2007-10-28

        Yes. I should have warned you that it was illustrative only. Sorry.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.