Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSString *' is disallowed with ARC - nsstring

For the following line of code I am getting the error below:
for (UILabel *label in labels) {
label.text = label.tag - 100 > someMutableString.length ? "" : "*";
}
The error states:
Implicit conversion of a non-Objective-C pointer type 'char *' to 'NSString *' is disallowed with ARC
My variable "someMutableString" is of type NSMutableString.
How do I fix in my particular case?

The problem is that your string literals are "" and "*" which are both C-style strings (const char*). So the type of the right hand side of the assignment is also const char*. You are assigning to the text property of a UILabel, which takes an NSString.
Use #"" and #"*" instead.

char *text = "a"
NSString *message = [NSString stringWithFormat:#"%s",text];
Cheers :)

Related

QString.toInt() doesnt work - Error: invalid operands to binary expression ('const char *' and 'const char [14]')

I have this code:
QString carda = "000123";
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda.toInt() + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");
and for the conversion from QString to Int when I use carda.toInt() Im having this error:
error: invalid operands to binary expression ('const char *' and
'const char [14]')
and warnings:
warning: adding 'int' to a string does not append to the string
use array indexing to silence this warning
I dont understand why QString.toInt() wont be working... any idea?
I dont understand why QString.toInt() wont be working... any idea?
the problem is that in qt you just can't concatenate together strings and numbers...
and actually you dont even need to convert the string carda to integer because that is a QString
instead just do:
QString queryStringAnet("SELECT * FROM [records] WHERE ([user]='" + carda + "' AND [apl]='"+apl+"' AND [tasktype]='"+taskType+"' AND [taskkind]='"+taskKind+"' AND [timestamp]='"+timestamp+"')");

arduino, error: invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'

I have a problem with some code.
Have read a tons of topics, but most are them are related to custom libraries.
My code is not related to any custom libraries.
I hope some of you know what im doing wrong.
I'm simply trying to "merge" two strings into a new variable.
Error:
sketch_SS01:13: error: invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'
char apiPath = apiPage + pid;
^
exit status 1
invalid operands of types 'char [14]' and 'char [5]' to binary 'operator+'
Error related to this code:
// api details
char apiPage[] = "/api.php?pid=";
char pid[] = "8855";
char apiPath = apiPage + pid;
The compiler says it: you cannot use operator+ to concatenate C strings (i.e. char[]). You need to use the library function strcat or it's safer sibling strncat.
The concatenation of a string x onto the string dest is strcat (dest,x); but please consult the documentation and pay extra attention to the risk of buffer overflows when dealing with char arrays.
To write your example as it is written you can do
// api details
char apiPage[] = "/api.php?pid=";
char pid[] = "8855";
char apiPath[100] = ""; // make sure it' long enough and initialized to empty string
strcat(apiPath, apiPage);
strcat(apiPath, pid);
or you could copy the strings using to the correct place in the destination string usingstrcpy or strncpy.
Addition:
A (perhaps better/simpler/safer) alternative is to use the String class which has all the expected string functionality (like constructors, add, append, etc.): see https://www.arduino.cc/en/Reference/StringObject

I have trouble in understanding the output of the following code

I have the following program :
int main()
{
char arr[] = "geeksforgeeks";
char *ptr = arr;
while(*ptr != '\0')
++*ptr++;
printf("%s %s", arr, ptr);
getchar();
return 0;
}
Output: hffltgpshfflt
Explanation given is :
If one knows the precedence and associativity of the operators then there is nothing much left. Below is the precedence of operators.
Postfixx ++ left-to-right
Prefix ++ right-to-left
Dereference * right-to-left
Therefore the expression ++*ptr++ has following effect :
Value of *ptr is incremented
Value of ptr is incremented
My question is how this pointer expression ++*ptr++ is getting implemented and why does this statement "printf("%s %s", arr, ptr);" not printing the string "geeksforgeeks" as well ?
Please help.
Answer to --> why does this statement "printf("%s %s", arr, ptr);" not printing the string "geeksforgeeks" as well ?
Here,array elements of arr are incremented by 1 i.e.,g+1=h,e+1=f.... so on this is getting incremented by 1 due to ++*ptr which increments the ptr value .
ptr++ will incremented by one it means the ptr address is incremented by '1'. until the null character.
So, you are printing arr it shows the value as hffltgpshfflt and printing the ptr which is now pointing to NULL which prints nothing. you can check the ptr value by %x format it prints 0.

How to append this in Qt?

I want to add a new line in this. This is my sample code:
ui->button->setText(" Tips " + "\n" + TipsCount );
This is the error it shows:
invalid operands of types 'const char [7]' and 'const char [2]' to binary 'operator+'
But when I add to label it gets appended!
ui->label->setText(name + "\n" + City );
Can someone please help me?
This is a very common problem in C++ (in general, not just QT).
Thanks to the magic of operator overloading, name + "\n" gets turned into a method call (couldn't say which one since you don't list the type). In other words, because one of the two things is an object with + overloaded it works.
However when you try to do "abc" + "de", it blows up. The reason is because the compiler attempts to add two arrays together. It doesn't understand that you mean concatenation, and tries to treat it as an arithmetic operation.
To correct this, wrap your string literals in the appropriate string object type (std::string or QString most likely).
Here is a little case study:
QString h = "Hello"; // works
QString w = "World"; // works too, of course
QString a = h + "World"; // works
QString b = "Hello" + w; // also works
QString c = "Hello" + "World"; // does not work
String literals in C++ (text in quotes) are not objects and don't have methods...just like numeric values aren't objects. To make a string start acting "object-like" it has to get wrapped up into an object. QString is one of those wrapping objects, as is the std::string in C++.
Yet the behavior you see in a and b show we're somehow able to add a string literal to an object. That comes from the fact that Qt has defined global operator overloads for both the case where the left operand is a QString with the right a const char*:
http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24
...as well as the other case where the left is a const char* and the right is a QString:
http://doc.qt.nokia.com/latest/qstring.html#operator-2b-27
If those did not exist then you would have had to write:
QString a = h + QString("World");
QString b = QString("Hello") + w;
You could still do that if you want. In that case what you'll cause to run will be the addition overload for both operands as QString:
http://doc.qt.nokia.com/latest/qstring.html#operator-2b-24
But if even that didn't exist, you'd have to call a member function. For instance, append():
http://doc.qt.nokia.com/latest/qstring.html#append
In fact, you might notice that there's no overload for appending an integer to a string. (There's one for a char, however.) So if your TipsCount is an integer, you'll have to find some way of turning it into a QString. The static number() methods are one way.
http://doc.qt.nokia.com/latest/qstring.html#number
So you might find you need:
ui->button->setText(QString(" Tips ") + "\n" + QString::number(TipsCount));

NSString stringWithUTF8String return null on device, but ok on simulator

I have a really weird problem. Basically just convert a char to nsstring and store them in an nsmutable array.
But the code runs ok on simulator, but crash on device.
Here is the crash code,
char t = 'A' + i;
NSString* alphabetString = [NSString stringWithUTF8String:&t]; //substringToIndex:1];
[tempArray addObject:alphabetString];
Basically the stringWithUTF8String will return NULL on device, but return valid value on simulator.
The device is an iPhone 4s.
I did not see any notification of changes on NSString stringwithutf8string on iOS5 release.
Thanks.
The address of a single char is not a C-style string. You need to ensure it's null terminated with something like:
char t = 'A' + i;
char s[2]; s[0] = t; s[1] = '\0';
NSString* alphabetString = [NSString stringWithUTF8String:s];
From the docpage:
Parameters
bytes : A NULL-terminated C array of bytes in UTF8 encoding.
You can't pass the address of a single char value to -stringWithUTF8String. That function is expecting a null-terminated string, and you're not passing it one. This results in undefined behavior: anything at all could happen. It might appear to succeed, it might fail benignly, or it might erase your file system. But more likely, it will just crash your program.
You should create a two-character array that's null-terminated instead:
char t[2] = {'A' + i, 0}; // Two-character null-terminated array
NSString* alphabetString = [NSString stringWithUTF8String:t];
Alternatively, you can also use -stringWithFormat: with the %c format specifier to get a string containing a single character:
NSString* alphabetString = [NSString stringWithFormat:#"%c", 'A' + i];

Resources