Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the meaning of that statement
while(*char)
{
//body of the loop
}


does it mean to loop through the array until the last element ?

What I have tried:

..................................................
Posted
Updated 25-Jun-21 11:03am
v2

Quote:
does it mean to loop through the array until the last element ?
Nope.
The loop body is executed if *char is non-zero.

This is generally used to loop over the content of a string until the string-terminator character (i.e. zero) is found.


You may try yourself the following program:
C
#include <stdio.h>
int main()
{
  char a[] = { 'f', 'o', 'o', 0, 'b', 'a', 'r' };

  int counter = 0;

  char * c = a;
  while ( *c )
  {
    ++counter;
    ++c;
  }

  printf("%d\n", counter);
  return 0;
}


Please note: the code you posted would not even compile, because char is a C reserved word.
 
Share this answer
 
Comments
Patrice T 25-Jun-21 17:04pm    
+5
CPallini 28-Jun-21 2:13am    
Thank you!
It's a look that looks at a string, character by character, until the character it's looking at is 0.

Any non-zero value returned by the pointer is seen as true and zero is seen as false.
 
Share this answer
 
Comments
CPallini 25-Jun-21 10:53am    
5.
OriginalGriff 25-Jun-21 10:53am    
Low-vote countered.
To add to what Dave has said, I'll add to things:
1) Your code won't compile: char is a reserved word and can;'t be used as a variable name.
2) I'd strongly recommend using an autoincrement there:
C
printf("Hello World\n");
char* ch = "abcde";
char c;
while(c = *ch++)
   {
   printf("%c\n", c);
   }
That makes it a lot easier to see what is happening, and that the loop will terminate.
 
Share this answer
 
Quote:
does it mean to loop through the array until the last element ?

No, it will loop until it encounter 0 in the array. Note that C/C++ do not know the size of an array.
If your array is zero terminated, the loop will end at zero.
In the era before keyword 'string', strings were stored as zero terminated char arrays.
To make sure what is going on in a piece of code, the tool of choice is the debugger.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900