Results 1 to 22 of 22

Thread: char arrays to strings

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46

    char arrays to strings

    i run vc++.net

    how do i convert an char array to a string, and vice versa? everything i've tried gives an error, like '= cannot convert from char(2) to char (string?)' and stuff like that.

    help puhlease!
    What does "formatting drive C..." mean?!

  2. #2
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Should be able to use:

    string myString = "my string";
    char *myCharstar = new char[myString.length()];

    strcpy(myCharstar, myString.c_str());

    string.c_str() converts a string to a const char*. Then the copy copies the string.

    OK?

    HD

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The other way round:
    Code:
    const char *cstr = "Hello";
    string str;
    str = cstr;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46
    guess what?

    about an hour after i posted (i was offline) i figured it out.
    btw, cornedbee, i found that, too, but it gave me a bunch of junk (y-> ) - or something like that after it. got any suggestions about that?

    here's what I had:

    Code:
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    #include <tchar.h>
    
    using namespace System;
    
    int _tmain(void)
    {
        char letters[5];
    	String* myword;
    	
    	letters[0] = 'h';
    	letters[1] = 'e';
    	letters[2] = 'l';
    	letters[3] = 'l';
    	letters[4] = 'o';
    	myword = letters;
    	Console::WriteLine(myword);
    	
        return 0;
    }
    What does "formatting drive C..." mean?!

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    char letters[6]
    ...
    letters[5] = 0

    Z.

  6. #6
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Sorry, I'm a bit of a retard Didn't read the post properly. Could have saved everyone a bit of time by shutting up........*slap*.


    HD

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Oh, .net String...
    We thought C++SL string.

    Well, it should work much the same way.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46
    well i found out i just needed to add __nogc[] after the variable identifier. now its just giving me grief in getting the string into the array. i suppose it would help if i remembered the suggestions above
    What does "formatting drive C..." mean?!

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You'd find it easier learning normal C++ before Managed C++ which is a nightmare for normal C++ programmers

    If you're going to use .NET, use C#. It's not actually a bad language (some bits of it are very neatly designed, IMO) and most of their stuff works with it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Unless you have a very special case where you need to mix managed and unmanaged code. C++ is the only .NET language capable of this.

    But for 99.9999% of the cases C# is the better choice.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C# can do pointers, I believe. You give your function the "unsafe" modifier. Although then you can only call it from another unsafe function :-/
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But only in C++ you can do things like mixing the managed (garbage collected) heap with a standard heap, directly calling WinAPI and such useless stuff.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yeah. Although with .NET you shouldn't really need to. And with Mono you won't be able to...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    whats managed/unmanaged c++/whats the diff?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  15. #15
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    and such useless stuff.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  16. #16
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Unmanaged C++ is traditional C++ the way we know it.

    Managed C++ is a special capability of the Visual C++.NET compiler. It means that C++ code is compiled to .NET MSIML (bytecode) instead of native code, uses the .NET CLR (framework) and GC (garbage collection). Beside the /clr compiler switch it also requires a few special keywords to be used, the most important being __gc for class and/or pointer declarations (a bit tricky). The #import directive and the using keyword are also important, as well as the __delegate keyword.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  17. #17
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    so it needs the .net runtimes?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  18. #18

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46
    well, im in a class that only teaches managed that im already past the drop point, so im not going to waste my money

    anyway, let me restate the problem, with the changes that ive discovered. remember, this is c++.net


    consider this:
    Code:
    __gc public class someclass
    {
    public:
    
       String* myMessage;
    
       someclass()
       {
          char tempMessage __nogc[] = myMessage;
          //do stuff to it
    //*
          myMessage = tempMessage;
       }
    };
    The line after the //* already works. its the first part that doesn't. so far, trying it like that gives me the fewest errors.
    btw, that strcopy method didn't work. i don't think the c_str function is there anymore. anyway, that gives me an error at the char array line:

    error C2440: 'initializing' : cannot convert from 'System::String __gc *' to 'char __gc[]'


    that is where the problem is at now
    What does "formatting drive C..." mean?!

  19. #19
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Try
    Code:
    char tempMessage __nogc[] = myMessage.ToCharArray();
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  20. #20

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46
    I tried that (except now it needs a -> operator instead of a . operator) and it said something like 'cannot convert from _w _char to char __nogc[]'. i don't think that's exactly what it said, but it's close.
    What does "formatting drive C..." mean?!

  21. #21
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sorry, don't know any more things...

    Oh yeah, you could try copying it to a __gc array and then use strcpy to copy the content of the __gc array to a __nogc array. I admit that's a pretty desperate idea.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  22. #22

    Thread Starter
    Member
    Join Date
    Oct 2002
    Location
    la la la you can't see me
    Posts
    46
    actually, i got help from a professor and ended up with this:

    Code:
    char myArray[5];
    String* myString = "hello";
    
    for(int x = 0; x < =4; ++x)
    {
      myArray[x] = myString->get_char(x);
    }
    at least you don't have to worry about it now
    What does "formatting drive C..." mean?!

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