When I recently look at some passage about C pointers, I found something interesting. What it said is, a code like this:
char var[10];
char *pointer = &v
Yes, you can go for it.
Please note that *pointer
is the value at the memory location the pointer
point to(or hold the address of).
Your *pointer
is now pointing to the individual characters of the character array var
.
So, while(*pointer)
is shorthand usage of the equivalent
while(*pointer!='\0').
Suppose, your string is initialized to 9 characters say "123456789" and situated at an address say addr
(memory location).
Now because of the statement:
char *pointer=&var;
pointer
will point to first element of string "1234567890".
When you write the *pointer
it will retrieve the value stored at the memory location addr
which is 1
.
Now, the statement:
while(*pointer)
will be equivalent to
while(49)
because ASCII Value of 1
is 49, and condition is evaluated to true.
This will continue till \0
character is reached after incrementing pointer
for nine times.
Now, the statement:
while(*pointer)
will be equivalent to
while(0)
because ASCII value of \0
is 0
. Thus, condition is evaluated to false and loop stops.
Summary:
In while(condition)
, condition
must be non-zero to continue loop execution. If condition
evaluates to zero
then loop stops executing.
while(*pointer)
will work till the value at memory location being pointed to is a non-zero
ASCII value.
Also you can use:
if(*ptr){ //instead of if(*ptr!='\0')
//do somthing
}
if(!*ptr){ //instead of if(*ptr=='\0')
//do somthing
}
Let's start with a simple example::
int a = 2 ;
int *b = &a ;
/* Run the loop till *b i.e., 2 != 0
Now, you know that, the loop will run twice
and then the condition will become false
*/
while( *b != 0 )
{
*b-- ;
}
Similarly, your code is working with char*
, a string.
char var[10] ;
/* copy some string of max char count = 9,
and append the end of string with a '\0' char.*/
char *pointer = &var ;
while( *pointer != '\0' )
{
// do something
// Increment the pointer 1 or some other valid value
}
So, the while loop will run till *pointer
don't hit '\0'
.
while( *pointer )
/* The above statement means the same as while( *pointer != '\0' ),
because, null char ('\0') = decimal value, numeric zero, 0*/
But the usage can change when you do, while(*pointer != 'x'), where x can be any char
. In this case, your first code will exit after *pointer hits the 'x' char
but your second snippet will run till *pointer hits '\0' char
.
*pointer
means exactly what it says: "Give me the value that's stored at the place that the pointer points to". Or "dereference pointer
" for short. In your concrete example, dereferencing the pointer produces the one of the characters in a string.
while(*pointer)
also means exactly what is says: "While the expression *pointer
yields a true value, execute the body of the loop".
Since C considers all non-zero values as true, using *pointer
in a condition is always equivalent to using the expression *pointer != 0
. Consequently, many C programmers omit the != 0
part in order to practice boolean zen.
*pointer
means dereference the value stored at the location pointed by pointer
. When pointer
points to a string and used in while
loop like while(*pointer)
, it is equivalent to while(*pointer != '\0')
: loop util null terminator if found.
while(x) {
do_something();
}
will run do_something()
repeatedly as long as x
is true. In C, "true" means "not zero".
'\0'
is a null character. Numerically, it's zero (the bits that represents '\0'
is the same as the number zero; just like a space is the number 0x20 = 32).
So you have while(*pointer != '\0')
. While the pointed-to -memory is not a zero byte. Earlier, I said "true" means "non-zero", so the comparison x != 0
(if x
is int
, short
, etc.) or x != '\0'
(if x
is char
) the same as just x
inside an if, while, etc.
Should you use this shorter form? In my opinion, no. It makes it less clear to someone reading the code what the intention is. If you write the comparison explicitly, it makes it a lot more obvious what the intention of the loop is, even if they technically mean the same thing to the compiler.
So if you write while(x)
, x
should be a boolean or a C int that represents a boolean (a true-or-false concept) already. If you write while(x != 0)
, then you care about x
being a nonzero integer and are doing something numerical with x
. If you write while(x != '\0')
, then x
is a char
and you want to keep going until you find a null character (you're probably processing a C string).