开发者

converting an array of characters to a const gchar*

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?


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";


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.


This may be really late, but in case anyone else stuck with this comes here with a google:

gtk_text_buffer_insert(GTK_TEXT_BUFFER(textbuffer),&iter,buffer,strlen(buffer));

should do the trick. The problem is the null character on the end. So by sending in the string length you send in all characters before the null character.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜