On 2 Mar, 20:12, "cman" <til...@gmail.comwrote:
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.