assigning to 'uint8_t *' (aka 'unsigned char *' ) from incompatible type 'void *' - objective-c

uint8_t *bufferPtr=NULL;
size_t bufferPtrSize=0;
bufferPtr=malloc(bufferPtrSize * sizeof(uint8_t))
I have used this same code in File1.m it is working fine. When I am using this on File2.mm it is giving me assigning to 'uint8_t *' (aka 'unsigned char *' ) from incompatible type 'void *'error.

This is because .mm files aren't treated as normal Objective-C files but as Objective-C++ files, so the type rules of C++ apply here. In C++, if you decide to have a type (in this case you have a void pointer) and then want another type (in your case a char pointer), then you have to explicitly cast them. The way to do this is static_cast<T>() where T is the target type. Ie: uint8_t *bufferPtr = static_cast<uint8_t *>(malloc(bufferPtrSize * sizeof(uint8_t)));

Related

Why can you declare an NSInteger with a string value?

Why is it that you can do something like this?
NSInteger something = #"something";
It feels weird that this doesn't throw an error and instead returns some number that, in fact, is an NSInteger.
For me it says Incompatible pointer to integer conversion initializing 'NSInteger' (aka 'int') with an expression of type 'NSString *'
I feel, what you get number is memory address of that NSString.
In Languages Like Objective C, When You Define a class or a constant string(What you define like #"something" is a constant string), it will be stored somewhere and the pointer to it will be used. Pointers are 32-bit and 64-bit. 32-bit pointers are 32-bit Integers so they can easily be casted to an integer. So there is no way to throw an error as it is a legal assignment.
it's the same line of code
NSInteger something = [#"something" integerValue];

format descriptor for unsigned short * in IOS 6

In my code I have some strings in unichar format with definition like below
unichar sourceString[100];
After I assign value to sourceString, I need to put it into an NSString with proper format descriptor.
Previously in IOS 5, I used %S, and it worked just fine. The code is as follows
NSString * targetString = [NSString stringWithFormat:#"%S",sourceString];
But after I upgraded SDK to IOS 6.0, warnings comes up with the same code:
**Format specifies type 'const unsigned short *' but the argument has type 'unichar *' (aka 'unsigned short *')**
My app still works but there are hundreds of warnings when the code is compiled, which is really annoying.
I am wondering what is the correct format descriptor I shall use in this case?
Thanks!
Using const unichar sourceString[100]; will solve your problem.
If you try using %hu the warning still appears?
Seeing format types at: http://reference.jumpingmonkey.org/programming_languages/objective-c/types.html

ARC Error on UIImageJPEGRepresentation

Been battling this one for a while. Basically, I am converting an image into NSData so I can send it to a server. The code I have used before, but for some reason I am getting an ARC error on this. The error lands on the line I declare the imageData variable.
NOTE: myImage is handed to the method.
- (void)uploadImage:(NSImage *)myImage {
NSData *imageData = UIImageJPEGRepresentation(myImage, 1.0);
// Do something...
}
I get an error and two warnings
Error: Implicit conversion of 'int' to 'NSData *' is disallowed with ARC
Warning: Implicit declaration of function 'UIImageJPEGRepresentation' is invalid in C99
Warning: Incompatible integer to pointer conversion intializing 'NSData * __strong' with an expression of type 'int'
Any ideas?
You might need to include the relevant header:
#import <UIKit/UIKit.h>
Prior to C99, if you call a function that the compiler hasn't seen a declaration for, it will compile the call as if the function was declared as int UIImageJPEGRepresentation(). C99 doesn't allow that, but it seems that the compiler is still applying the old interpretation (or the compiler is in pre-C99 mode; I'm not sure what the default is), hence the ARC error.

Unicode formatting compiler warning: Format specifies type 'unsigned short' but the argument has type 'int'

It's a tad OCD, but I hate getting any compiler warnings. When I updated XCode, I started getting this compiler warning:
Format specifies type 'unsigned short' but the argument has type 'int'
When I tried including the Unicode character for degree using the following code:
currentVal = [NSString stringWithFormat:#"%.2f%C", angleDeg, 0x00B0];
How do I make the compiler warning go away, either by changing the code or turning off that particular compiler warning?
Cast the literal to unichar:
currentVal = [NSString stringWithFormat:#"%.2f%C", angleDeg, (unichar)0x00B0];

Objective-C; typedef objc_object as substitute for id without pointer;

In Objective-C id is a typedef:
typedef struct objc_object {
Class isa;
} *id;
So I can declare (and initialize) a variable e.g. like this:
// using id
id po_one = #"one";
Compiles fine.
Since I name my types in an own scheme and since I dislike the implied pointer in the id typedef I want to add an own typedef (with O for object) like this:
typedef struct objc_object O;
So a variable declaration (with initialization) could look like:
// using O
O * po_two = #"two";
This compiles with a warning:
Initialization from incompatible pointer
type
As far as I understand typedefs I thought that the latter should be equivalent to:
// using struct objc_object
struct objc_object * po_three = #"three";
Which again compiles fine.
It's astonishing that:
po_two = po_one;
po_two = po_three;
both compile without warnings.
So I tried:
typedef struct objc_object * PO;
to see whether it works without warning if I include the pointer (being exactly the thing I want to avoid - so just for test reasons) to see whether a typedef outside of the structure definition works differently to a typedef in the structure definition (which is the definition of id in this case).
// using PO
PO po_four = #"four";
That also works fine.
If I use:
#define O struct objc_object
the construct using O again compiles without warning.
But I prefer to use typedefs instead of a defines whenever possible.
I'm puzzled. What is my misunderstanding with typedefs?
I'm not an expert of compilers, so probably a real expert can give you a better answer. In any case, based on my knowledge, when a compiler does type checking, it bases this check on the "parse tree", which generates a sort of table for all possible data type structures, and check if two definitions point to the same row in the table. So it works by fetching symbols and type definitions from this table and comparing them.
Now "struct objc_object" is one of these data structures. Then, when id is defined, it generates another entry: "id --> struct objc_object *".
The po_three definition leads to the same reference: "struct objc_object *" (infact "=" has lowest precedence).
The same happens for po_four, because PO leads by definition to the same reference, again (PO --> struct objc_object *).
When you use the definition with #define, thanks to preprocessing you're still referencing "struct objc_object *".
But when you use O * po_two = #"two" you're in fact trying to match one type which is "id" (#"two"), that is "struct objc_object *" and another one which is a pointer to "struct objc_object" but even if logically they are the same they don't lead to the same reference in the table. The reason is because "O" type has been defined yet, so po_two is simply stored in the symbol table as a pointer to "O" and "O --> struct objc_object". And that's the reason of the warning: po_two is a pointer to something different than #"two".
I have a curiosity: have you tried to plot the symbol table using "nm"?