Advertisement

char to char *

Started by July 19, 2000 03:46 PM
22 comments, last by KalvinB 24 years, 5 months ago
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



define the length with a variable at run time....

Im Always Bored
--Bordem
ICQ: 76947930
Im Always Bored--Bordem ICQ: 76947930
Advertisement
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);}    

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 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?

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).
Thanks! I''ll try those out and see what happens.

Ben
Advertisement
    #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
The C++ string class has a [] operator, so using str[j] should work fine.
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.
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
~V'lionBugle4d

This topic is closed to new replies.

Advertisement