Initialize the results array so all elements are NULL:
char* results[10] = { NULL };
In the posted code the elements are unitialized and will be holding random values.
Additionally, prevent going beyond the bounds of the results array:
while (i < 10 && result)
{
}
Answer from hmjd on Stack OverflowI'm new to C and I'm trying to iterate over an array and when all values up to max size are assigned I have no problem but when I leave any values empty and do the for loop it takes some junk values from memory. I was wondering if there is some "end value" like \0 for string to know where to stop.
Sorry for the bad english and thank you in advance
Initialize the results array so all elements are NULL:
char* results[10] = { NULL };
In the posted code the elements are unitialized and will be holding random values.
Additionally, prevent going beyond the bounds of the results array:
while (i < 10 && result)
{
}
There's no such thing as an "empty array" or an "empty element" in C. The array always holds a fixed pre-determined number of elements and each element always holds some value.
The only way to introduce the concept of an "empty" element is to implement it yourself. You have to decide which element value will be reserved to be used as "empty value". Then you'll have to initialize your array elements with this value. Later you will compare the elements against that "empty" value to see whether they are... well, empty.
In your case the array in question consist of pointers. In this case selecting the null pointer value as the reserved value designating an "empty" element is an obvious good choice. Declare your results array as
char * results[10] = { 0 }; // or `= { NULL };`
an later check the elements as
if (results[i] == NULL) // or `if (!results[i])`
/* Empty element */
An array in C++ cannot be null; only a pointer can be null.
To test whether a pointer is null, you simply test whether it compares equal to NULL or 0.
Array in C++ cannot be "empty". When you define an array object, you explicitly specify the exact size of the array. That array contains (and always will contain) that exact number of elements you specified in the definition. No more no less. It will never be "empty".
You're right, that is a pointless check, if done in the same context.
If you pass it as parameter to a function, the the array decays into a pointer and it's not so pointless.
what should go there instead to check the array is empty
Well, the array is never empty. It will always contain MAX_LOGPATH_LEN TCHARs. But the following could do the trick:
if( m_sLogPath[0] == _T('\0') || _tcsicmp(m_sLogPath, trace_path)!=0)
The m_sLogPath array always contains MAX_LOGPATH_LEN characters. Assuming that MAX_LOGPATH_LEN is a constant greater than 0, then the array will never really be empty.
I'm guessing that what you really want to check is whether the C-style string stored in this array is empty. If that's the case, then the simplest way to do it would be:
#include <tchar.h>
#include <windows.h>
const size_t MAX_LOGPATH_LEN = MAX_PATH;
TCHAR m_sLogPath[MAX_LOGPATH_LEN];
int main()
{
if (m_sLogPath[0] == _T('\0'))
{
// m_sLogPath contains empty string.
}
}
What do you mean with empty?
When a C program is executed, variables that you don't explicitly initialize have got unpredictable values.
You need to set all of your array cells to NULL (or to 0, or to whatever value represents emptyness in your program logic) and then you can check it in the way you did:
int *array[3] = { NULL, NULL, NULL }; // array of three "empty" pointers
...
for( i = 0; i < 3; ++ i ) {
if( array[i] == NULL ) {
// i-th cell is "empty"
}
}
Question answer:
What you posted is the correct code.
Elaboration:
If it "doesn't seem to work", perhaps the problem does not lie at this location in your code. If you would post a more complete example of what you have, the code's expected behavior and actual behavior, we may be able to help you.