Testing pointers in while-loops

C

cman

If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.

cman
 
C

Chris Thomasson

cman said:
If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Well, first is dereferences current, then tests what it pointed to against
0. This should promptly SEG-FAULT in your example though...

;^(
 
S

santosh

cman said:
If you have the following string variable declared:

char *current;

This is not a string variable. In fact C doesn't have a string type.
What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.

The statements within the while loop, if any, will be executed if
value yielded by deferencing current is zero.
 
G

gw7rib

If you have the following string variable declared:

char *current;

What does the following test in a while-loop amount to:

While (!*current)

Does this check if *current is "not empty" i.e not null or does it
suggest something else.

cman

If you do something like:

char *current = "Hello";

then three things will happen. Firstly, the computer will reserve
enough space to store 6 characters (usually 6 bytes of memory).
Secondly, it will fill them up - the first five with the codes
representing the letters, and the sixth with 0 to show the end of the
string. And thirdly, it will set current to be the address of the
first one.

If you do:

while(!*current)

then this will continue as long as *current is 0. So if current is
pointing at (say) a letter, the loop will stop. But if current is
pointing at a 0, say the 0 at the end of a string, the loop will keep
going. It seems a bit of a strange thing to want to do but there may
be a use. For instance, you might do:

char current[20];

// read in a string into current here
while(!*current) {
// print message complaining that the user has
// typed in an empty string
// read in another string
}

Note that this is not the same as current being NULL. If current is
NULL this means it does not point at anything - not the begining of a
string, not the end of a string, nowhere.

Here's an example using a slightly different bit of code - note the
lack of a ! :

#include <stdio.h>

int main(void) {

char *current = "Test";

while(*current) {
printf("char %c\n", *current);
current++; }

return 0;
}

This prints out the characters of the string one at a time, and stops
when it hits the 0 at the end of the string.

Hope that helps.
Paul.
 
C

CBFalconer

cman said:
If you have the following string variable declared:

char *current;

This is not a string variable. If you read what you wrote you
defined a pointer to a char.
What does the following test in a while-loop amount to:

While (!*current)

It may launch WWIII, polish your shoes, vomit in your lap, etc.
Dereferencing an uninitialized pointer invokes undefined
behaviour. You have also failed to define the function "While",
which is not the keyword "while".
 
D

Default User

Chris said:
Well, first is dereferences current, then tests what it pointed to
against 0. This should promptly SEG-FAULT in your example though...

Should it? Why? There is a wide range of things that undefined behavior
can cause. There's no "should" about that.




Brian
 
C

Chris Thomasson

Default User said:
Should it? Why? There is a wide range of things that undefined behavior
can cause. There's no "should" about that.

Great point! I stand corrected; thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top