Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatonating strings

Status
Not open for further replies.

michaelkrauklis

Programmer
Dec 5, 2001
226
US
This might be more of a straight C++ or even C issue, but maybe there's an easy way to do it in VC++. Does anyone know of a way to easily concatonise two char pointers? something like strcat is optimal, but strcat only accepts const char pointers, where my char pointers aren't constant. If there's a standard function or an easy way to do this please let me know. Thank you. MYenigmaSELF
myenigmaself@yahoo.com
 
strcat should work fine.

the first agument of strcat is a char* and the second is a const char*

Just make sure your first agument has enough space to concatanate the two together.

Matt
 
Yeah, I was looking at strcmp, but I still have the same problem. Neither of my strings are const. This seems like a juvenile question but how do you create a dynamic char * with a static size? That seems like a contradiction of terms. Here is my code:
Code:
try{
	char *sql="CREATE TABLE ";
	sql=strcat(sql,source);

}
catch(...){
	cerr<<&quot;error:2&quot;;
}
//where source is a char* passed to the function this code
//is in
When I run this it just prints out &quot;error:2&quot; so obviously there is an error when I call strcat(because source isn't const, right??).
Eventually I would like to be adding more and more to the first string in a while loop. The amount I am going to be adding is dependant on the file I'm reading in from, and memory is an issue so I would like to only allocate as much as I need. Is there a way that you can get the length of a string without traversing char by char and looking for the NULL char? If so I guess I can do this somewhat easily myself with calloc and realloc. Thanks for the help. MYenigmaSELF
myenigmaself@yahoo.com
 
Ok, I'm sorry. I've got this backwards. It's been like three years since I've programmed in C or C++. The char * isn't constant forever, only while it is being used by strcat, being that the first string is being changed and the second string is not. But what did you mean by making sure there is enough space in the first char *? The space is determined by how long the string is when I create it. I also figured out that I can use strlen to figure out the string length so using dynamic memory alocation I can change the size of these strings... correct? I've been spoiled by Java. I think I've got it figured out though. Any pointers will still be appreciated. Thanks

MYenigmaSELF
myenigmaself@yahoo.com
 
Ok, just to further clarify I got it all figured out and working:
Code:
	char *sql=(char*)calloc(15+strlen(source),8);
	sql[0]=NULL;
	sql=strcat(sql,&quot;CREATE TABLE &quot;);
	sql=strcat(sql,source);

then I just have to realloc for all additional concatonizations. If any of this is redundant let me know, but it looks good to me. Thanks for your time. MYenigmaSELF
myenigmaself@yahoo.com
 
your problem is that your destination is already constant...
it has a constant SIZE!!

you defined it as &quot;CREATE TABLE &quot;.
you need to define it as sql[80] or something like that and then give it its value.
you can even do sql[80]=&quot;CREATE TABLE &quot;
and dont forget dont do *sql but sql, or you'll have to cast types.

for your source if its really dynamically its obviously not a problem. but maybe it will require casting types.

and dont do:
sql=strcat(sql,source); !!!!!!!!!
first, because you double the function's time. it returns what it enters to the first parameter anyway.
second, because you cant enter a *char type to a char[80] type... you will get an error.

strcat will change the 1st parameter (sql) immediatly, and then also returns it. and if you do 1st=strcat(1st,2nd) so its like to do sql=sql;

Daniel Cohen Gindi
dcgsmix@gmx.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top