tags:

views:

117

answers:

2

I've got an array of characters which contains a string:

char buf[MAXBUFLEN];
buf[0] = 'f';
buf[1] = 'o';
buf[2] = 'o';
buf[3] = '\0';

I'm looking to pass this string as an argument to the gtk_text_buffer_insert function in order to insert it into a GtkTextBuffer. What I can't figure out is how to convert it to a const gchar *, which is what gtk_text_buffer_insert expects as its third argument.

Can anybody help me out?

+5  A: 

gchar is just a typedef for char and the conversion from arrays to const pointers is implicit, so you can just pass it:

someFunctionExpectingConstGcharPointer(buf);

Note that you can also directly initialize arrays with string literals:

char buf[MAXBUFLEN] = "foo";
Georg Fritzsche
+1  A: 

Well, lets see. GTK+ reference manual says:

gtk_text_buffer_insert ()

void gtk_text_buffer_insert (GtkTextBuffer *buffer, GtkTextIter *iter, const gchar *text, gint len);

Inserts len bytes of text at position iter. If len is -1, text must be nul-terminated and will be inserted in its entirety. Emits the "insert-text" signal; insertion actually occurs in the default handler for the signal. iter is invalidated when insertion occurs (because the buffer contents change), but the default signal handler revalidates it to point to the end of the inserted text.

buffer : a GtkTextBuffer
iter : a position in the buffer
text : UTF-8 format text to insert
len : length of text in bytes, or -1

and

gchar
typedef char gchar;
Corresponds to the standard C char type

This means, that a char* can be used in place of gchar*.
Though I have not used gtk_text_buffer_insert() , but in other gtk+ functions that require gchar*, char* has worked flawlessly.

Hope it was helpful.

Andrew-Dufresne