Why can not I use || for pointer?

2

I have the following code:

int i=0;
variable a;
a.type = CHAR;
a.name = malloc(sizeof(char)+1);
while(*l->str++ != ' ');
while(*l->str != ';' || *l->str != '='){
    a.name = realloc(a.name, ((!i)?1:i)*sizeof(char)+1);
    a.name[i] = *l->str;
    i++;
    *l->str++;
}
a.name[i] = '
int i=0;
variable a;
a.type = CHAR;
a.name = malloc(sizeof(char)+1);
while(*l->str++ != ' ');
while(*l->str != ';' || *l->str != '='){
    a.name = realloc(a.name, ((!i)?1:i)*sizeof(char)+1);
    a.name[i] = *l->str;
    i++;
    *l->str++;
}
a.name[i] = '%pre%';
printf("%s\n", a.name);
'; printf("%s\n", a.name);

But it gives segment fault .

When I remove the *l->str != '=' or the *l->str != ';' from the while condition it works normally. I would like to know why you give segment fault and if there is any way with if to solve.

    
asked by anonymous 07.03.2014 / 05:37

1 answer

6

It seems to me that your problem is that your while has no exit condition: it will continue forever! (or rather continue to give a segfault) Suppose your string is simply:

;=

Using while(*l->str != ';') what happens?

  • Is it different from ; ? No. Then exit the loop.
  • Already using while(*l->str != '=') :

  • Is it different from = ? IS. Go to the next
  • Is it different from = ? No. Then exit the loop.
  • But using while(*l->str != ';' || *l->str != '=') :

  • Is it different from ; ? No, but is it different from = ? IS. Go to the next
  • Is it different from ; ? IS. Go to the next
  • Segmentation fault
  • That is, the problem lies in your condition. See if what you really want is not a && .

        
    07.03.2014 / 06:01