problem in while loop

In the fuction given, can anybody explain how there is no condition in while but still it works.

1
2
3
4
5
6
7
8
9
10
11
12
13
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
 
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}
original and add could be conditions, we cant know that becuase we cant see them in this code you've provided.

It looks like their chars. This

1
2
3
4
while ('h')
{
	int a;
}


Will work, Although Ive got no idea what it means :D
Last edited on
Heres the complete code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
 
void concatenate_string(char*, char*);
 
int main()
{
    char original[100], add[100];
 
    printf("Enter source string\n");
    gets(original);
 
    printf("Enter string to concatenate\n");
    gets(add);
 
    concatenate_string(original, add);
 
    printf("String after concatenation is \"%s\"\n", original);
 
    return 0;
}
 
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
 
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}
There is a condition in each. Remember that there exists an implicit conversion from char to bool.

The conversion is essentially
IF char's value is '\0'
THEN return false
ELSE return true



PS. Your function can be used only if the array that original points has enough elements to hold also the contents of the add's C-string.
I think the first loop will run as long as the *original is not "\0", the second will run as long as *add is not "\0" This is the condition. The "\0" should be found at the end of each string.
Last edited on
Wait i dont get how there is a conversion from char to boolean?
(sorry i m new to this)
@alexBB
Yeah i get that but i want to know the logic behind it.
We can write while(*original !=\0) which would make sense but why does while(*original) means the same thing?
Logical false is represened by "\0"

According to keskiverto :

http://www.cplusplus.com/forum/beginner/159922/

the NULL pointer is represented by "\0"

the end of string is represented by "\0"
Last edited on
Topic archived. No new replies allowed.