-5

Why do I get a segmentation fault when I send the flags -h or -H.

bool parse_command(int argc, char **argv, bool *header, char **fileName)
{

    if (!argv)
        return false;

    bool *flagh = false;
    bool *flagH = false;

    char *options = "Hh";
    int opt = 0;

    while ( (opt = getopt(argc, argv, options ) ) != -1)
    {
        printf("HELLO");
        switch (opt)
        {
            case 'h': 
                *flagh = true; 
                break;
            case 'H':
                *flagH = true; 
                break;
            default:
                usage_p1();  
                return false;
        }

    }
    printf("%d", opt);
    // Implement this function
    return true;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
  • `bool` and `true` is not keywords in C. Are you using a C++ compiler? – klutt Jan 22 '18 at 22:23
  • The asterisk in front of `flagh` suggest that it's a pointer - that you don't allocate. – 500 - Internal Server Error Jan 22 '18 at 22:24
  • This souldn't even compile, or at least not without warnings. flagh and flagH are pointers to booleans, but they don't point to anything. You don't have any actual booleans in memory. You then assign them (the pointers) to "false", which essentially make them point to memory address 0, thus the segfault. – Lee Daniel Crocker Jan 22 '18 at 22:25
  • You define two variables that are *pointers* but your initialization of them doesn't make them point to a valid location. It seems to me that those variable should *not* be pointers. – Some programmer dude Jan 22 '18 at 22:25
  • @VladfromMoscow I cannot see a single global? – klutt Jan 22 '18 at 22:26
  • Turn on all warnings on your compiler. Read the warnings. They should give you a hint about what's wrong. – klutt Jan 22 '18 at 22:26
  • @klutt You are right but it is unimportant.:) – Vlad from Moscow Jan 22 '18 at 22:27
  • 2
    @klutt They are, however, defined in `stdbool.h` – Christian Gibbons Jan 22 '18 at 22:32
  • @ChristianGibbons yes, but they are no keywords, the are just macros that have the value of 0 and 1. – Pablo Jan 22 '18 at 22:46
  • @Pablo Well `true` is defined as `1`, but `bool` is defined as `_Bool` which is a keyword. Regardless, keyword or not, they are pretty well-defined values and their existence specifically as keywords don't really matter here and don't really indicate the use of c++. – Christian Gibbons Jan 22 '18 at 22:55

2 Answers2

6

These two lines are your problem:

bool *flagh = false;
bool *flagH = false;

You're declaring flagh and flagH as pointers to booleans, but they don't point anywhere yet. In fact they point definitively nowhere, since your initializations are equivalent to

bool *flagh = NULL;
bool *flagH = NULL;

You probably don't want these to be pointers. Change the declarations to

bool flagh = false;
bool flagH = false;

Change the assignments to

flagh = true; 

flagH = true; 
Steve Summit
  • 45,437
  • 7
  • 70
  • 103
2

Take a look at this:

bool *flagh = false;
bool *flagH = false;

Both variables are pointers, you initialize them with false. There are no true or false in C, instead something is considered false when it evaluates to 0, and something is considered true when it isn't false.

If this is true C code, then it would be the same as doing

bool *flagh = NULL;
bool *flagH = NULL;

Later on you do

*flagh = true;

which is dereferencing a NULL pointer, which is undefined and will result in a segfault.

To fix your code:

#include <stdbool.h>

bool flagh = false;
bool flagH = false;

and then late

flagh = true;
flagH = true;

// or

flagh = false;
flagH = false;

Like many said in the comments, C doesn't have a true boolean type. See: Using boolean values in C

edit

Nowadays there is stdbool.h which declares a type bool and true and false, but all is doing is redefining true as 1 and false as 0:

stdbool.h

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#define bool  _Bool
#define true  1
#define false 0

#else /* __cplusplus */

/* Supporting _Bool in C++ is a GCC extension.  */
#define _Bool bool

#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension.  */
#define bool  bool
#define false false
#define true  true
#endif

#endif /* __cplusplus */

/* Signal that all the definitions are present.  */
#define __bool_true_false_are_defined 1

#endif    /* stdbool.h */
Pablo
  • 13,271
  • 4
  • 39
  • 59