For some reason CString IsEmpty keeps returning true even though the CString is obviously not empty.
CString temp = "Hello";
if (temp.IsEmpty)
AfxMessageBox("temp is empty");
else
AfxMessageBox("temp is not empty");
Any reason why this is the case?
Perhaps:
if(temp.IsEmpty())
not
if(temp.IsEmpty)
Iam not totally sure but try this:
CString temp( "Hello" );
I found in this link https://msdn.microsoft.com/en-us/library/aa314317%28v=vs.60%29.aspx
Look at the end, it says
CString city = "Philadelphia"; // NOT the assignment operator
Hope this helps!:)
Related
In Swift I am using C API that returns struct with char array (containing UTF8 null terminated string or null).
struct TextStruct {
char * text;
//other data
}
I use:
let text: String = String(cString: data.text)
This works, however, when data.text is nullptr, this fails with
fatal error: unexpectedly found nil while unwrapping an Optional value
Is there any workaround, or I have to check data.text manually before using cString ctor?
In addition to Gwendal Roué's solution: You can
annotate the C API to indicate whether the pointer can be null or not.
For example,
struct TextStruct {
char * _Nullable text;
//other data
};
is imported to Swift as
public struct TextStruct {
public var text: UnsafeMutablePointer<Int8>?
// ...
}
where var text is a "strong" optional instead of an implicitly
unwrapped optional. Then
let text = String(cString: data.text)
// value of optional type 'UnsafeMutablePointer<Int8>?' not unwrapped; ...
no longer compiles, and forces you to use optional binding or
other unwrapping techniques, and the "fatal error: unexpectedly found nil"
cannot happen anymore accidentally.
For more information, see "Nullability and Objective-C" from the Swift Blog –
despite the title, it can be used with pure C as well.
Yes, you have to check data.text, in order to make sure it can feed the String(cString:) initializer, which is documented to require a non-null pointer.
A technique is to use a if let statement. This is a classic technique for safely unwrapping optional values:
let str: String?
if let ptr = ptr {
str = String(cString: ptr)
} else {
str = nil
}
print("got \(str ?? "nil")")
Another technique is the Optional.map function:
let str = ptr.map { String(cString: $0) }
print("got \(str ?? "nil")")
You can check if the address of the pointer is set
let textIsSet = Int(bitPattern: data.text) != 0
let text: String? = textIsSet ? String(cString: data.text) : nil
In my function I want to return an empty string "", function looks like this:
char *myFunction(nodeType *arg){
if (something){
return anotherFunction(arg);
} else {
return EMPTYSTRING;
}
}
(EMPTYSTRING should be replaced with correct expression to return "")
Some ways I came up with:
return ""
return '\0'
What is the right way to return an empty string?
It is not good idea to return "". Because, it is const char and you may try to dealloc it in where you get it from the function and this likely crashes your application.
So, I would return strdup(""), so you can free it safely.
return '\0' is not correct; it is a null character.
return "";
is more correct.
And you can make a small test function to test returning an empty string, before your data structure is implemented.
\0 is indeed a null-pointer constant, not an empty string, so returning that would still return a null pointer.
However you've got another problem here.
If anotherFunction returns a string that you don't need to free() then it is correct to return "" here. If it returns a dynamically malloc()ated string that you must free(), then you also need to return a dynamically allocated empty string here - return calloc(1, 1); would do the trick.
Hi everyone I have a problem taking care of a malfunction, I'm building a dynamic linked list which connects structures with *next and *previous,
now if the structures are unable to connect (long story) I need to print "the way does not exist" now I've made an if statement
temp = point_start; // point_start is the head of the list
condition = 2;
for(i=0;i<gagnum;i++) //gagnum is the number of structures
{
temp=point_start->previous; // previous is the pointer of the previous structure.
if (temp != 0xcdcdcdcd) // ------> is this legal?
condition = 1;
else
{
printf("no route found ")
condition = 2;
break;
}
}
I have an if statement which says "if (temp != 0xcdcdcdcd)" so when it gets garbage I put out the message and stop the program, is this legal to use? is there a better way to do this? thank you so much for your help!
Instead of using 0xcdcdcdcd, you should be using 0x0 to indicate a NULL pointer.
Here's a function which does what I think you're trying to do:
// checks to see if the given list is the "first" structure
bool is_first_structure(void* head)
{
return head->previous != NULL;
}
And here's how to use it:
if(!is_first_structure(head))
{
printf("no route found");
}
Hi the answer to the problem is just to NULL all your pointers when you're building the linked list. then check if the pointer is NULL instead of garbage, hope it helps!
Wikipedia writes for 0xcdcdcdcd
Used by Microsoft's C/C++ debug malloc() function to mark uninitialized heap memory, usually returned from HeapAlloc()
So I guess you malloc a node and not initializing the pointers.
Thank you, it works ....................................................................
strcpy(holder[pos].key, new.key);
holder[pos].position = new.position
strcpy(holder[pos].name, new.name);
strcpy(holder[pos].country, new.country);
You don't need strcpy or any special function for that. Strcopy is only necessary because strings aren't strictly just a data type in C, but null-terminated char arrays.
You can do it like this:
holder[pos].position = new.position; // Assigns the value of new.position to holder[pos].position
please use
holder[pos].position = new.position
I have the following structure and have some code to use it below. The control is not going into the if statement(I need to check if the chat_info[queue].message is empty which it is)
struct chat{
char message[MAXNAME];
int client;
int group;
int flag;
};
.
.
.
.
if(filedata.data==NULL)
{
printf("\n data is %s",filedata.data);} //displays "data is "
chat_info[queue].message[0]='\0'; //setting 0 before copying data
strcpy(chat_info[queue].message,filedata.data);
printf("\n data is %s",chat_info[queue].message);//displays "data is "
if(chat_info[queue].message[0]=='\0'){
// not going into this if statement
//I also tried if(chat_info[queue].message== "") and if(chat_info[queue].message== NULL)
}
The first issue I see:
if (filedata.data == NULL)
Which can also be written as:
if (!filedata.data)
Once inside the if-statement you attempt to copy the contents of filedata.data into chat_info[queue].message. However, we previously established that filedata.data points to nothing. It is NULL. Using strcpy() with a NULL pointer as the source should not work.
Perhaps you meant:
if (filedata.data != NULL)
Which can also be written as:
if (filedata.data)
Secondly, if filedata.data wasn't NULL, think about when chat_info[queue].message[0] == '\0' would be true.
strcpy(chat_info[queue].message, filedata.data);
if(chat_info[queue].message[0] == '\0') {
// Message was determined to be empty.
}
It would only be true if the filedata.data was an empty string. (Note: this is different than being NULL!) Is that what you want? If so, the current code should work fine.
Reference: strcpy() documentation
What is the type for message? Because, if it is a string, then it will implicitly cast when you set it to a char = '\0';. Perhaps, you need to do:
if (chat_info[queue].message[0] == "\0")
strcpy(chat_info[queue].message,filedata.data);
/* You have assigned some data for chat_info[queue].message
* in the above step assignment should be successful.
*/
if(chat_info[queue].message[0]=='\0')
/* How can message[0] be null? It will never be. Think of what you're
* trying to achieve with the condition.
* If you're checking for a non-empty message, then it should be
* chat_info[queue].message[0]!='\0'
*/
{
./* some stuff here */
}