how to check if _variant_t is NULL - c++

I am reading data from excel cell via ADO
while (!pRec->adoEOF)
{
_variant_t a = pRec->Fields->GetItem(long(0))->Value;
//todo
}
how to check if a equal NULL
a.bstrVal==NULL
it didn't work.

I suspect you want:
a.vt == VT_NULL // Represents a NULL received from a database
Or
a.vt == VT_EMPTY // Represents an uninitialized object
Database Nulls have a specific representation in VARIANT.
However if it is from Excel it's also possible you are getting an empty string rather than a null.
a.vt == VT_BSTR && SysStringLen(a.bstrVal) == 0

Related

Inserting node in Hash table with open addressing [Optimizing the logic]

I am trying to understand a data structure, hash table with open addressing.
I am currently reading on the source code provided by geekforgeeks, but I have a few questions on the code.
Below, is the pasted function for inserting Node from geekforgeeks.
//Function to add key value pair
void insertNode(K key, V value)
{
HashNode<K,V> *temp = new HashNode<K,V>(key, value);
// Apply hash function to find index for given key
int hashIndex = hashCode(key);
//find next free space
while(arr[hashIndex] != NULL && arr[hashIndex]->key != key //// LINE 9 //////
&& arr[hashIndex]->key != -1)
{
hashIndex++;
hashIndex %= capacity;
}
//if new node to be inserted increase the current size
if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1) //// LINE 17 //////
size++;
arr[hashIndex] = temp;
}
Questions
In line 9, why would you check three conditionals, being,
if slot inside the hash table is null ===> arr[hashIndex] != NULL
AND if slot has the same key with the node that is going to be inserted ===> arr[hashIndex]->key != key
AND if slot has the key of -1, which indicates the slot where node was deleted before ===> arr[hashIndex]->key != -1
If I were to optimize this code, I believe checking whether the slot is NULL or not is already enough.
In line 17, why would you increment the size property of HashMap before assigning the node to the slot? ===> if(arr[hashIndex] == NULL || arr[hashIndex]->key == -1)
size++;
To me, this logic seems to be messy.
I would rather do, arr[hashIndex] = temp; size++;
With the assumption of geekforgeeks's logic is well written, could you explain to me why the logic for inserting the new node to a hash table with open addressing is implemented as above specifically on the two points I have raised?
The three conditions to have a valid index are:
The object at the index is NULL
OR the object is not NULL, but its key is the same of the one we're inserting
OR the object is not NULL, but its key value is -1
Since the negation of all three conditions occurs, we don't have a valid index, and the loop rolls on.
In line 17: size is incremented only if the insertion doesn't reuse an existing index, so the node is new (which means either condition 1 or 3 applies).

Parsing RapidJSON string returns NULL on brackets

{"1":"value","data":[A,B,C]}
1 returns value.
data returns null.
Adding quotations "[A,B,C]" isn't an array anymore.
rapidjson::Value &arr = document["data"];
if( !arr.IsArray() || arr.Size() != 3 )
{
Return;
}
else
{
x = arr[rapidjson::SizeType(0)].GetDouble();
y = arr[rapidjson::SizeType(1)].GetDouble();
z = arr[rapidjson::SizeType(2)].GetDouble();
};
How should I properly handle the [bracketed array]?
I have quite a lot of objects without quotes. Can I handle the objects without quotes?

sqlite and possible null value

Sqlite3 table have INT column with possible NULL values.
I add the data to the table with this code:
if ( ptr == NULL )
sqlite3_bind_null(stmt, 5);
else
sqlite3_bind_int64(stmt, 5, ptr->key_session );
then I get back my values with the following code:
unsigned int key_session = (unsigned int)sqlite3_column_int64(stmt, 0);
So, what is about NULL? What is the best practice with NULLs in the table? how to check for NULL? or what will occur in my GET code with NULL ?
You can do this check:
if (sqlite3_column_type(stmt, 0) == SQLITE_NULL)
...
If the check succeeds, the value was NULL; otherwise, it was a non-null int64.
You can check whether sqlite_column_type() returns SQLITE_NULL.
If 0 is a valid return for nulls, you don't need to do anything special - nulls will convert to zero if an integer is requested.

iterate char** why does this work?

I picked up a this piece of code I copy past to my program. This seems to be a new way to me to iterate through char**:
char** vArray; // The array containing values
// Go throught properties
if(szKey == "KeyMgmt")
{
vArray = (char**)g_value_get_boxed((GValue*)value);
for( ; vArray && *vArray ; vArray++) // Why does this work ?!
pWpaKey->addKeyMgmt(std::string(*vArray));
}
else if(szKey == "Pairwise")
{
// ...
}
It looks like to work like a charm but I don't understant why! vArray is Supposed to contain an adress right? And *vArray the "string" value. So why when I "AND" an address with its value this give me an equality?
vArray && *vArray is equivalent to (vArray != NULL) && (*vArray != NULL)
It's first checking that the pointer vArray isn't NULL and, assuming it is not NULL, checking that the pointer it points to isn't NULL.
The loop condition is
vArray && *vArray
This is basically shorthand for
(vArray != 0) && (*vArray != 0)
which is true if the char** pointer is non-null and points to a char* which is non-null.

linux c/c++ - weird if/else issue

I'm querying a mysql table which then loops through the results.
One of the fields has a value of "0" in it, so when I try the following it doesn't work!
while ((row2 = mysql_fetch_row(resultset2)) != NULL) {
if (row2[2] != "0") {
// the field has a value of 0, but it's still executing the code here!
} else {
// should be executing this code
}
}
I know C/C++ is very strict when it comes variables (unlink php), but I can't figure this one out. Anyone have any ideas why?
You're comparing row2[2], a pointer to char, with a pointer to the constant char array "0".
Use strcmp(row2[2], "0") != 0 (C solution), std::string(row2[2]) != "0" (C++ solution), or atoi(row2[2]) != 0 if you know row2[2] is always the string representation of an integer (and cannot be a SQL NULL value).
You cannot compare string literal like this :
if (row2[2] != "0") //wrong
Either you do this :
if (strcmp(row2[2], "0")) //correct
Or this:
if (std::string(row2[2]) != "0") //correct
For this particular case, when there is only one character you can also do this:
if (row2[2][0] != '0') //correct - not the single quote around 0!