Re: [gtk-list] strings



On Jul 19,  9:26pm, Bob P wrote:
> Subject: [gtk-list] strings

> in vb
> strings were rather simple.

That's VB for you ... :-)

>  basically, what's the
> difference between a string declared as:

First off, if you're not comfortable with strings yet perhaps you should be
making sure you understand C completely before you start using GTK+. Otherwise
you'll get twisted when it comes to things like callbacks and more thorny
pointer issues. I don't mean this in the nicest possible way. C *is* a low-
level language.

If I were you I'd read comp.lang.c answers as well as one of the good C
tutorials you'll see on the Web.

In any case ... first off, gchar is simply a glib/gtk+ typedef for char; it's
there for portability and consistency. So we'll ignore gchar and concentrate on
char.

> char some_string[10];

Allocates an array of char. You can "some_string" as if it were a char *
(pointer to character) in nearly all places, except (1) you can't free it
with free(), and (2) you can't reassign it to "point" somewhere else. It
isn't a pointer, it's an array name that behaves like a pointer.

> char *some_string;

some_string is a pointer to a character. This can be assigned to point to
anything you like, including the address value returned by a call to malloc().
But note that no memory is allocated when you declare it, so you'll have to
do it yourself.

> GString *somestring = NULL;

Don't forget that when you create an automatic variable, its value is
undefined.
It might just point into that section of memory that contains an account
number or something. It is "dangling".

Dangling pointers are dangerous; it's common usage to declare them and
immediately assign the NULL pointer to them. NULL is special. You can
never dereference it, and trying to do so will cause the program to
terminate. It's a lot easier to notice that somestring is 0x0 in your
debugger than somestring = 0x327f2a00. The programmer in this case is simply
saying, "I'm declaring a pointer, but to protect myself, I'm making sure
it's not pointing anywhere that might cause problems later".



Regards,

Ricky

-- 

Fixed Income Research Group  Tel: +44 [0]171 774 8527
Goldman Sachs International  Fax: +44 [0]171 774 8821



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