Quantcast
Channel: User Alex Lop. - Stack Overflow
Viewing all articles
Browse latest Browse all 39

Answer by Alex Lop. for recurrent decent expression parser yeilding a zsh: segmentation fault

$
0
0

If you enable compiler warnings, you will see that you pass pointer to pointer to struct Node when calling for parse_atom():

parse_atom(expr, &Root);

while it accepts only a pointer to struct Node (https://godbolt.org/z/ud8-hX):

void parse_atom(char *expr, struct Node *Root);

Which invokes the following warning on clang:

warning: incompatible pointer types passing 'struct Node **' to parameter of type 'struct Node *'; remove & [-Wincompatible-pointer-types]

parse_atom(expr, &Root);                 ^~~~~

:19:42: note: passing argument to parameter 'Root' here

void parse_atom(char *expr, struct Node *Root) {

                                   ^

Just pass the pointer itself as Root:

parse_atom(expr, Root);

Additional note. Look at this code:

        struct Node L, R;        Root -> op = '+';        Root -> Left = &L;        Root -> Right = &R;

This works because everything runs in the scope of parse_add() function.Once you are out of this function, the addresses of L and R will point to invalid locations as those object would not exist anymore.


Viewing all articles
Browse latest Browse all 39

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>