3

My application reads settings from a conf file first, and then those options can be overwritten from the cli arguments. After it loads the settings from the conf, I need to check if the require values are set but I'm stuck at making it check the variables.

Sample code:

#include <stdio.h>

int main() {

const char* test;

if (test != NULL)
   std::cout << test << "\n";
else
   std::cout << "no value set\n";

return 0;
}

What did I do wrong?

haylem
  • 22,460
  • 3
  • 67
  • 96
codefrog
  • 611
  • 3
  • 10
  • 13
  • 4
    What's the concrete problem? What happens? What *should* happen? (You didn't initialize `test`.) – Flinsch Dec 02 '10 at 17:50
  • re-tagged to c as well. Even though there are C++-specific stuff here (only the streams, really), the question (and the answer) is C-oriented. – haylem Dec 02 '10 at 18:33

4 Answers4

7

You didn't initialize test. If you want it to be NULL initially, you have to set it:

const char* test = NULL;
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • But if I initialize it as NULL, doesn't that mean I won't be able to change its value later (because it's const char*)? – codefrog Dec 02 '10 at 17:53
  • 2
    @code: No, you have a pointer to `const char` here. Even if it were a `const` pointer (`const char* const`) you can only set its value by initializing it and couldn't modify it later on anyway. – Georg Fritzsche Dec 02 '10 at 17:55
  • 1
    It's `const char*`, not `char* const`. A const pointer is not the same as a pointer to const. – Philipp Dec 02 '10 at 18:11
  • @codefrog: [This question](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const) should clear things up regarding `const` and pointers. – Georg Fritzsche Dec 02 '10 at 18:14
  • @codefrog - see this previous question for background on your question here. http://stackoverflow.com/questions/2156305/double-const-declaration – Steve Townsend Dec 02 '10 at 18:15
0

C and C++ do not initialize pointers to NULL automatically. If you do not assign it a value, it still has a value, it is just an unknown, indeterminate value. It could be NULL, it could be something else.

In your sample code, test has a value, but it is unknown. So your if-statement might or might not be true.

abelenky
  • 63,815
  • 23
  • 109
  • 159
0

As an alternative that is more idiomatic for C++, you could use a string and check whether it's empty after your init completes:

#include <string>

std::string test;   // default constructor produces an empty string

// do the config load

if (test.empty())
{
  // config error
}

Note that if the data semantics here include an empty value being legitimate, this alternative is not viable.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • 1
    A string is not in general the same thing as an optional string. Sometimes "set to empty" doesn't mean the same thing as "unset", so it's more about the data model than it is about idiom. – Steve Jessop Dec 02 '10 at 18:10
  • @Steve - that's true - if the data semantics here include an empty value being legitimate, this alternative is not viable. – Steve Townsend Dec 02 '10 at 18:11
  • In that case, with that said, `string` is indeed more idiomatic than `char*` to contain a string in C++. Questioner: use it where possible! – Steve Jessop Dec 02 '10 at 18:13
0

You don't.

You cannot check a "defined" state in C/C++ if you haven't "defined" that state yourself. Don't leave initialization up to your compiler implementation if you are doing something like this.

Initialize your pointer to NULL.

haylem
  • 22,460
  • 3
  • 67
  • 96