Archived

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

KalvinB

char to char *

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
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!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
    
char *str, ch;
int j;

for (j=0;j<10;j++)
{
ch=j+48;
strcat(str,(char *)&ch);
}


cout<<str;


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 this post


Link to post
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 this post


Link to post
Share on other sites
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
start with the address of ch and try to read sequential
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 <iostream.h>
# include <stdlib.h>
# include <string.h>

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 this post


Link to post
Share on other sites
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 <string> for this

std::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 either

cout << str.c_str();


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

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
    
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

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<<str<<"\n";

cout<<"\n\n";
while (*str)
cout<<*str++;

cout<<"\n\n";

for (j=0;j<128;j++)
str2+=j+48;

cout<<str2.c_str();

}


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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 will

myStr += "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;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

Share this post


Link to post
Share on other sites
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:"<<pMsg->dwType<<" Size:"<<dwMsgSize<<" From:"<<idFrom<<" To:"<<idTo<<"\n";

LogFile<<pMsg->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

Share this post


Link to post
Share on other sites