Results 1 to 4 of 4

Thread: Convert char* to char[] ?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Convert char* to char[] ?

    I know this should be easy but I can't figure it out, how can I convert a char* to a char[] array? I'm not using CLR or MFC.

    Here's what I have so far, using main ( int argc, char* argv[] )



    char inchar[33];
    char tempchar = 'a'; /* dummy value to start */
    int count = 0;

    while ( tempchar != '\0' )
    {
    tempchar = argv[1][count];
    inchar[count] = tempchar;
    count = count + 1;
    }

    fprintf(stdout, inchar);



    When I run it like PROG.EXE DOG

    I get only D as the output ( the first character only every time ).

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Convert char* to char[] ?

    I compiled and ran your code with no problems, it printed "Dog".

    There is actually no real difference between an array of char or a pointer to char, so no conversion should be necessary.
    Have you debugged the application and stepped through the loop to see if you can gather any clues as to why its behaving like that?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Convert char* to char[] ?

    Weird. Its Visual Studio 2008, a console application ( not CLR or MFC ), this is the exact code, I put the print out of the argument right below the main:

    int _tmain(int argc, char* argv[])
    {

    fprintf(stdout, argv[1]);

    ...


    Typing PROG.EXE DOG still shows just D

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Convert char* to char[] ?

    Oh Got it. When I created the Application it was something like this:

    int _tmain(int argc, _TCHAR* argv)



    Changing it to the old way worked ( I had to change the function name too from _tmain to main )

    int main(int argc, char* argv[])

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width