concatenating strings

C

clinisbut

I'm not sure if this is the right group, but I didn't found any other
more appropiate to post my problem.

I'm trying to concatenate chars using the Glib library and I'm getting
strange behaviours.

This is my code:

int num_chars = strlen( text );
vector< gchar* > sourceStrings;
gchar* stringTemp;
gchar* phrase = NULL;

for( int i=0; i<num_chars; i++ )
{
if( text!='\n' )
{
stringTemp = g_strconcat( phrase, text, NULL );
printf("stringTemp: %s\n",stringTemp);
g_free( phrase );
phrase = stringTemp;
printf("phrase: %s\n\n",phrase);
}
else
{
sourceStrings.push_back( phrase );
g_free( phrase );
phrase = NULL;
}
}

Variable "text" is a gchar*.
g_strconcat should allocate a new gchar* with all strings passed
concatenated.
What I'm getting is g_strconcat is not concatenating anything, and
stringTemp is empty at first printf (null).
I think the problem comes from variable text, 'cause I test with this:

stringTemp = g_strconcat( "hello", text, NULL );

And this throws me an error at runing time, something related with
strlen().
 
V

vippstar

I'm not sure if this is the right group, but I didn't found any other
more appropiate to post my problem.

I'm trying to concatenate chars using the Glib library and I'm getting
strange behaviours.

This is my code:
<C++ code snipped>

Amazing, a programmer who doesn't know what language he uses :).
This newsgroup discusses C. Your code is (most likely) C++. However,
<comp.lang.c++> does not seem appropriate as only standard C++ is
discussed there. I don't know where to direct you, but you could wait
for Mr Twink, he has a bad habit of answering the most off-topic
questions.
 
C

clinisbut

On said:
<C++ code snipped>

Amazing, a programmer who doesn't know what language he uses :).
This newsgroup discusses C. Your code is (most likely) C++. However,
<comp.lang.c++> does not seem appropriate as only standard C++ is
discussed there. I don't know where to direct you, but you could wait
for Mr Twink, he has a bad habit of answering the most off-topic
questions.

I know there is some C++ code there, but my question relates uniquely
to C (Glib is "plain" C), so I wrote here this topic.
As you can see I don't ask anything about std::vector (I think is the
only C++ part in my code), that could give you a reason about
redirecting me to comp.lang.c++. I know this group only discuss C
standard, but as I said, I didn't found any better appropiate group to
post my doubt, so I'm sorry if my problem bothers you
 
F

Flash Gordon

clinisbut wrote, On 22/04/08 11:42:
I know there is some C++ code there, but my question relates uniquely
to C (Glib is "plain" C), so I wrote here this topic.
As you can see I don't ask anything about std::vector (I think is the
only C++ part in my code), that could give you a reason about
redirecting me to comp.lang.c++.

So why didn't you simply remove the C++ code? Otherwise how are we to
know that C++ is not doing something strange?
I know this group only discuss C
standard, but as I said, I didn't found any better appropiate group to
post my doubt, so I'm sorry if my problem bothers you

You could try the Gnome development forum since Glib is I believe part
of the Gnome project http://gnomesupport.org/forums/
Alternatively check what mailing lists exist.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top