beaver-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Beaver-devel] beaver_box_prompt


From: Double 12
Subject: Re: [Beaver-devel] beaver_box_prompt
Date: Sat, 13 Sep 2008 21:59:17 +0200
User-agent: Thunderbird 2.0.0.16 (X11/20080724)

Surprisingly, gtk_entry_get_text returns a *const* gchar*. This is because the function returns a pointer to a string that "points to internally allocated storage in the widget and must not be freed, modified or stored". I saved the result of the gtk_entry_get_text function in a const gchar* and used that as return value for the function beaver_box_prompt_impl. The weird thing is: if I print the text (extracted from the entry) directly in the beaver_box_prompt_impl function, it is displayed correctly. If I try to print the text in the function it is called from (sample_clicked in sample.c), I get weird characters. You will see it it if you download the latest commits from the CVS and then compile.

Double 12

Tobias Heinzen schreef:
Double 12 wrote:
I have worked on the beaver_box_prompt function. The box works fine, but getting the inputted value to the rest of the program doesn't really. It is still not clear to me of which type the return value of the function should be (gchar, gchar* or const gchar*) to transfer the string in the right way. I hope you can teach me something about it ;)
gchar: single character
gchar*: pointer to the first character of a sequence of characters (aka. "string")

the const keyword is something like a write protection. if I declare a variable const, i can't overwrite it's value.

example. if i got a variable

const gchar foo = 'a';

then i can't do something like this after it's declaration.

foo = 'b';

I'm still able to read from it but not write. this is often used for parameters in functions, where this parameter is only used for reading. this is especially used when you do not want accidently overwrite the pointer. but carefull. even if I declare a variable

const gchar* foo = "Hello";

i still can do something like this

gchar* bar = foo;
bar[0] = 'B';

With pointers only the variable holding the pointer is write protected but not the data the pointer is pointing to.

since you get the data from a text entry widget (or something like that), you will receive a gchar* anyway (look at the gtk documentation what the functions return. mostly you want to return the same thing). gchar* is probably the right choice here (since i think you can't return a const variable in C). Also you have to check wheter or not the returned pointer has to be freed after use (that should also be written in the gtk documentation. most of the gtk get_text () (or something like that) functions require to free the space after use).

greetings
Tobias


_______________________________________________
Beaver-devel mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/beaver-devel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]