LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-30-2004, 10:52 PM   #1
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Rep: Reputation: 30
if (text && *text) - ???


This is a function from a GTK+ tutorial:

static void toggle_show_text( GtkWidget *widget,
ProgressData *pdata )
{
const gchar *text;

text = gtk_progress_bar_get_text (GTK_PROGRESS_BAR (pdata->pbar));
if (text && *text)
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pdata->pbar), "");
else
gtk_progress_bar_set_text (GTK_PROGRESS_BAR (pdata->pbar), "some text");
}


Basically I know that it says: if there is text on the progress bar then change to "" else to "some text".

But I don't understand this:
if (text && *text)

Why are there both text and *text in this statement?
 
Old 09-30-2004, 11:22 PM   #2
Sadrul
LQ Newbie
 
Registered: Mar 2004
Distribution: Slackware 10.0 (K: 2.4.26)
Posts: 13

Rep: Reputation: 0
checking `text' for non-zero value to make sure that it is pointing to some location, checking `*text' for non-zero value to make sure that it has not reached the end of the string.

-- Adil
 
Old 09-30-2004, 11:22 PM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
text is a pointer to a string - the value of text is either a memory address, or null. The first half of the if tests whether text contains an actual address. testing *text then tests the value at that location - this will succeed if there is any data there, or fail if the string is empty (the value at *text will be 0 to terminate the string)

So if( text && *text ) literally says "if text is a valid pointer AND text points to a non-empty string".
 
Old 09-30-2004, 11:58 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Everything said above is right, just one more interesting thing.
In the statement (text && *text) the second part '*text' will not be evaluated if the first part 'text' is NULL, sometimes called "short circuiting AND logic". This prevents a runtime core dump in the case when text is NULL, because evaluating *(NULL) is not allowed. Normally and logically, AND operations can be performed in any order and get the same result, but not in this case because the second '*text' can not be evaluated if the first part is false.
This is a very important point and a frequent source of software bugs.
 
Old 10-01-2004, 10:07 AM   #5
greg108
Member
 
Registered: Aug 2003
Location: CA USA
Distribution: FC2, FC4, Mandrake 10, Slackware 10, RedHat 9, Suse 9.1, College Linux, Debian Sarge, Gentoo
Posts: 170

Original Poster
Rep: Reputation: 30
Thank you everyone,

The answer given by randyding was what I needed.

Just to confirm that I understand. I can't eveluate *text without evaluating text because that't the rule. The pointer has to be tested before the value that pointer points to is tested. Do I get it right?


Also if I do like that:
if (*text)
then it still compiles, but I get segmentation fault during execution. What exactly happens?
 
Old 10-01-2004, 12:52 PM   #6
paulymath
LQ Newbie
 
Registered: Oct 2003
Posts: 4

Rep: Reputation: 0
answer to: what happens if you dereference text without testing to see if it's NULL?

The compiler will let you dereference the pointer, but if it turns out to be NULL (an invalid address for you to dereference), your program will end abruptly (segmentation fault).

Testing to see if that pointer is NULL or not is not enforced by the compiler, but is never a bad idea. It's a short operation for the computer to perform, and can be the difference between gracefully handling the error or crashing without output.

Even if you know the source of the pointer, the "is this pointer NULL" test (immediately before the pointer's use) is good practice. If the code is shuffled around in the future, or copied by someone else, the source of the pointer may change. For the 2 dead-simple CPU instructions that will compile into (compare and conditional branch), just test the pointer.

-Paul
 
Old 10-01-2004, 12:59 PM   #7
paulymath
LQ Newbie
 
Registered: Oct 2003
Posts: 4

Rep: Reputation: 0
additional comment re: dereferencing pointers.

Just another note while you're reading about what you can and can't do with pointers:

NULL (zero) is not the only value of a pointer that you're not allowed to dereference. Any value of pointer that doesn't fall within a specific range of addresses will cause a segmentation fault when dereferenced (dereferencing, by the way, is the fancy say of saying "we check the contents of the address the pointer is equal to"). An invalid pointer can be the result of an uninitialized variable, incorrect pointer arithmetic, indexing an array improperly, or memory corruption elsewhere in the running process. The first three ways are preventable through good practice and correct code; the last one normally is too, but could potentially be caused by faulty code/libraries elsewhere.

This is an overkill response, I know, but there are going to be novice programmers reading this thread, and these are all good things to know when tracking/avoiding bugs

-Paul
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
phpldapadmin & clear text cookies [GOD]Anck Linux - Security 4 01-31-2005 07:41 AM
add arrows & text to photos Rick Winters Linux - Software 1 11-09-2004 05:25 PM
add arrows & text to photos Rick Winters Debian 1 11-09-2004 03:35 PM
Mozilla Thunderbird && plain text messages. Bogdan Linux - Software 1 02-03-2004 10:25 AM
[C & Linux] Read line from text-file Y_Haarman Programming 5 11-08-2002 04:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration