Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

c - 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] = '';

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...