-1

I have a piece of legacy code that has got char* function arguments which are used for if-then-else logical flow. For example:

void myFunc(char *f_reset) {

     .....
     .....
     if(*f_reset) {// do this;}
     else {// do that;}

}

suppose I am calling myFunc(char *f_reset) from main()

 void main(void) {
     char r = 0;
     char *f_reset = &r;

     *f_reset = 0;

     myFunc(f_reset);  // Debug and enter this function

  }

When I try to enter the function call, I find out that *f_reset is never evaluated as 0 i.e. false - it is always true because of some garbage -8342345825 or something like that.

The reason it is using char* is probably because in the old days boolean took more memory than char *? Some stuff I read in the past in Stackoverflow posts.

Could someone give me a dummy's guide for the relationship between char* and logical true false?

Haris
  • 12,120
  • 6
  • 43
  • 70
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • Zero literal '0' does not evaluate to zero/false? – Shaggi Oct 10 '13 at 07:11
  • Try `'\0'`, or simply `0`. – WhozCraig Oct 10 '13 at 07:16
  • @Shaggi no it doesn't. I have used 'H', 'L', 'h', 'l', but nothing. If you write a quick small program with an if-then-else based on a `char *` and debug it, it will show you the ascii number, not what you want. Frustrating isn't it? – ha9u63a7 Oct 10 '13 at 07:17
  • @WhozCraig I am sure 0 doesn't work. I am dead sure. – ha9u63a7 Oct 10 '13 at 07:18
  • 2
    Well, then you're wrong as well as dead-sure. `char ar[2], *p = ar; *p = 0; if (*p){} else{}` will *not* execute the if-block. `*p` equates to false. Anything besides a full-zero octet equates to true. Perhaps posting a *real* sample that *compiles* and demonstrates your problem may shed some light? – WhozCraig Oct 10 '13 at 07:19
  • @hagubear I know, i pointed it out to the op.. maybe not in a obvious way – Shaggi Oct 10 '13 at 07:36
  • @hagubear is the purpose of the boolean evaluation to see if the first octet in the data addressed by the passed-in parameter non-zero? if it is, then you're doing it right. If it isn't then you need to explain what you *do* want clearer. as posted now, it is correct to check the first `char` of the memory addressed by `f_reset`, and if it is non-zero, the if-block executes. If all you want to do is pass a boolean flag, is there a reason you're passing by address? A by-value parameter (`int` for example) seems it would be a much better fit. – WhozCraig Oct 10 '13 at 07:50
  • WhozCraig if you pass `*p` as an pointer argument now in a function, you will see that the `*p` will be evaluated as -823423847234 or some big horrible number which is logical TRUE and my reset will always be TRUE! which is not right! And yes, I am using `char *` because it is from legacy code written about 15 years ago. I feel ashamed to use char * for logical. Boolean is the best fit for it. – ha9u63a7 Oct 10 '13 at 07:56
  • You better look again. that big hairy value you're talking about is not `*p`, its **`p`**. Its the address held *in* the pointer variable. Dereferencing that address in your `if` condition will give you the `char` value you're looking for. As written you're code will execute the `else` block in `myFunc`. – WhozCraig Oct 10 '13 at 08:07
  • @WhozCraig I know what I mean.... *p dereference it to the value and p means the address where the value is located. if(*p) means if the value addressed by p is a logical true or 1. My understanding of pointer referencing and dereferencing is not flawed! – ha9u63a7 Oct 10 '13 at 08:12
  • [**See It Live**](http://ideone.com/tAG13M). Apparently they are. Unless you're on a system where `char` is 32bit/64bit, your observation of the value of your dereferenced `char *` is lacking something. *What is the problem*??? If that `char` at that address evals non-zero, the `if` block fires, otherwise the `else` block fires. – WhozCraig Oct 10 '13 at 08:20

3 Answers3

1

In C there is not the boolean type. The if statment evaluete only intergers, so integer value 0 is false, anything else is true. The char type can be used as 8bit integer.

The char value:

char var = '0';

correspond to the integer value 48 (see an ASCII table) and not the integer value 0.

In your example you have a char pointer. But in your if statment your are not evaluating the pointer, but its value; so, *f_reset must be 0 (integer) to be evaluated as false.

char var = 0;

...
myFunc(&var);
...
Federico
  • 3,782
  • 32
  • 46
0
#include<stdio.h> 
#define TRUE 1
#define FALSE 0
void myFunc(char *);
int main()
{
char r = FALSE;
char *f_reset = &r;



myFunc(f_reset);  // Debug and enter this function

}

void myFunc(char *f_reset)

{
if(*f_reset)
printf("true");
else
printf("false");

}
Sarwan
  • 595
  • 3
  • 8
  • 21
  • @hugbear You can use this piece of code for sake of simplicity if you want to use boolean as char pointer.Assigning r=TRUE evaluates to true and vice versa – Sarwan Oct 10 '13 at 09:15
-1
#include<stdio.h>
int main()
{
char *x;
char a=0;
x=&a;

if(*x)
printf("true");
else
printf("false");

return 0;
}
Sarwan
  • 595
  • 3
  • 8
  • 21
  • Try this piece of code n you will get output as false. However assigning a='0' will give the output as true because it will be converted to its ascii value. – Sarwan Oct 10 '13 at 07:34
  • `@Sarwan` I know, but if you then pass `*x` to a function, it still evaluates as TRUE, I want it to be false. – ha9u63a7 Oct 10 '13 at 07:42
  • @hagubear what does "pass \*x" to a function it still evaluates as TRUE *mean* ? As I said before, your posted code doesn't even *compile* and isn't even valid C. If you posted a functioning-but-failing example, often called an [SSCCE](http://www.sscce.org), in your question, along with what you *want* to happen but isn't inline with the code (in comments ideally), it will be *much* easier to understand your goal and why you're not reaching it. – WhozCraig Oct 10 '13 at 07:48
  • @WhozCraig The code snippet is now updated. and my code compiles fine. What I want is a solution how I can correctly use logical true/false using char* in C code. – ha9u63a7 Oct 10 '13 at 08:01
  • @hagubear its looks good. the `f_reset` variable in `main` is somewhat pointless, as you could just pass `&r` and be done with it. I.e. `char r = 0; myFunc(&r);` But as-written, it should do what you want, in this case the `if (*f_reset)` should eval to false, and the else-block should hit. – WhozCraig Oct 10 '13 at 08:04