#### Archived

This topic is now archived and is closed to further replies.

# char to char *

## Recommended Posts

KalvinB    102
I''m using DirectPlay and I need to beable to construct messages of variable length at run-time using a char * variable. I''m getting all kinds of errors attempting to do this and I havn''t found any examples anywhere. Ben

##### Share on other sites
linternet    344
I''m not sure that I completely understand the question but you should be able to use malloc to allocate character arrays of different sizes.

ex:

char *data;

data = ((char *) malloc (size));

##### Share on other sites
KalvinB    102
I''m basically trying to create a variable that acts like a file.

Ben

##### Share on other sites
look up:

strdup, strcpy, strcat, strstr, strchr...

Converting a char to a char* is easy...

char MyChar;
char *pMyChar;

pMyChar = (char *) &MyChar;

Hope this helps!

----------------------------------------------
That's just my 200 bucks' worth!

##### Share on other sites
KalvinB    102
  char *str, ch; int j; for (j=0;j<10;j++) { ch=j+48; strcat(str,(char *)&ch); } cout<

I need this to print out "0123456789" however, the above gives me errors.

Also, how do I empty the pointer. I get a few junk characters when I convert just one character.

Ben

##### Share on other sites
Guest Anonymous Poster
I always store strings using the string class. To use it you must #include

To convert a string to char* you simply do mystring.c_str()

Easy or what?

You really should not use malloc or free. Use proper c++ and enjoy new/delete and the wonderful STL!

##### Share on other sites
linternet    344
KalvinB: your character pointer has no memory allocated to it, try the following:

char *str, ch;
int j;

/* allocate 11 for the last null character */
str = ((char *) malloc (11));

/* this will ensure that you have the null character at the
end of the string */
memset (str, 0, 11);

for (j=0;j<10;j++) {
ch=j+48;

/* strcat appends one string onto another, ch is not a string
it is a character, the line as you have it written will
characters until it hits a NULL (\0) character. This
will cause your program to access invalid memory
strcat(str,(char *)&ch); */

/* use */
str[j] = ch;
}

/* stubborn c programming verison = printf ("%s",str) :-) */
cout<

/* do more stuff with str*/

free (str); /* free the pointer when you''re done */

Forgive me, but I am a stubborn C programmer and I am proud of it!

##### Share on other sites
linternet    344
PS: How do you get those cool looking code boxes?

##### Share on other sites
Nazrix    307
  you use [ source ] [ / source ] without the spaces 

##### Share on other sites
blide    122
  Like this? 

-blide
blide@mail.com

##### Share on other sites
KalvinB    102
Having to define any sort of length defeats the purpose of what I''m trying to do.

I''m trying to have it so that I can write to a variable like I write to a file. I don''t have to define how many characters I''m going to allow in a file.

Ben

##### Share on other sites
Bordem    122
define the length with a variable at run time....

Im Always Bored
--Bordem
ICQ: 76947930

##### Share on other sites
linternet    344
At some point, you''re going to have to tell the computer how much memory you want from it. You can use realloc to keep extending the memory you have while leaving your original memory intact, however, it''s typically not the most efficient way to go.

  # include # include # include void main(){ char *str = NULL; char ch; int j; for (j = 0; j < 10; j++) { ch = j+48; str = ((char *) realloc (str, j+1)); str[j] = ch; } /* allocate the last one to add the NULL character */ str = ((char *) realloc (str, j+1)); str[j] = ''\0''; cout << str; free (str);} 

##### Share on other sites
Sheltem    122
If you do not want to specify the size of the string at any time, then you''re going to have to create a character array that has "more than enough" elements. Then you can use "strcpy" and "strcat" or whatever to your heart''s content without ever chaning the size of the variable. If you are adding elements yourself one at a time, you will probably need to append a 0 (the value 0 not the ASCII 0) at the end. The string functions do that for you. Also, remember these functions do absolutly no array bounds checking so make sure the array is like I said "more than enough". Or just use string class:

  //you need to include for thisstd::string str;int j; for (j=0;j<10;j++) { //"+=" is for appending to the string str += j+48;}//c_str member function is used to get c style string out of the//class for functions that need it. no worrying about zero-terminating it eithercout << str.c_str(); 

You''re already using "cout" to make console I/O easier. Why not use "string" to make strings easier?

##### Share on other sites
linternet    344
I will admit (though I don''t have to like it), that the C++ string class is the easiest and most efficent way to do what you want.

In my defense, the code I posted does work, and will allow you to construct strings of variable size (the ''\0'' character is equivalent to the zero value).

##### Share on other sites
KalvinB    102
Thanks! I''ll try those out and see what happens.

Ben

##### Share on other sites
KalvinB    102
  #include #include #include #include #include void main(){ std::string str2; char *str=NULL; char ch; int j; for (j=0;j<128;j++) { ch=j+48; str=((char *) realloc(str,j+1)); str[j]=ch; } str=((char *) realloc(str,j+1)); str[j]='\0'; cout<

How do I go about reading individual characters with the second method?

Ben

Edited by - KalvinB on July 20, 2000 5:48:38 PM

##### Share on other sites
linternet    344
The C++ string class has a [] operator, so using str[j] should work fine.

##### Share on other sites
Sheltem    122
You access individual elements just like you would a character array:

  std::string str;int j;for (j=0;j<10;j++) str += j+48;str += "789";//this will output "3"cout << str[3];

Note, that you can not access or write to elements of the array beyond the length of the current string. Doing +, +=, =, etc. add to the string length but doing str[4] = ''a'' does not. You can change elements of the string up to the length of the string - 1. The minus 1 is because the length() function also counts the 0 at the end of the string.

##### Share on other sites
Vlion    151
strings are never defined for me when i try to use them...
I #include and but still, it wont work. gives me a symbol not defined msg.
~V''lion

I came, I saw, I got programmers block.
~V''lion

##### Share on other sites
a2k    124
lemme try:
  #include void main(){cout << "cool!" << endl;}

a2k

Edited by - a2k on July 21, 2000 1:10:00 PM

##### Share on other sites
Wilka    122
Vlion:

The standard string class (defined in ''string'', ''string.h'' is the C string functions) lives in the std namespace. You can write ''std::string'' when you want a string, or add ''using std::string'' at the top of the file and just use ''string'' without the std:: when you need it.

##### Share on other sites
Yanroy    122
It seems to me that a CString will solve all your problems. It is variable length, and you can use "normal" operators on it. If you are using MSCV++ just look up CString in the help and under one of the related links it tells you what file to include.

  CString myStr;myStr += "Hello";myStr += '' ''; // I am not quite sure this will work... I think it willmyStr += "World";

After that code, myStr would hold "Hello World", and you never had to do anything related to telling it how much memory you need

--------------------

You are not a real programmer until you end all your sentences with semicolons;

Visit the ROAD Programming Website for more programming help.

##### Share on other sites
KalvinB    102
I tried the CString but afx.h is giving me linking errors.

std::string for some reason is failing to get the message across in DirectPlay.

  (CLIENT)struct GAMEMSG_GENERIC{ DWORD dwType; std::string message; };GAMEMSG_GENERIC msgWave;msgWave.message.erase();msgWave.message += "hello you server you.";msgWave.dwType = 1;g_pDP->Send( g_LocalPlayerDPID, 1, 0, &msgWave, sizeof msgWave) );(SERVER)HRESULT HandleAppMessages( HWND hDlg, GAMEMSG_GENERIC* pMsg, DWORD dwMsgSize, DPID idFrom, DPID idTo ){LogFile<<"Type:"<dwType<<" Size:"<message.c_str()<<"\n";

The server''s log file correctly shows the type, size, from and to but the message is empty.

I''m assumming I have to convert the message to a char * and send that accross and then convert back to read it.

DirectPlay has no problem getting char* sent.

Ben