Cannot initialize a variable of type 'void *' with an rvalue of type 'void *const *' - objective-c

I am trying to partially file a buffer in core-audio for iOS, in order to do this I need to change the start address I pass to memcpy. My code looks like this...
UInt32 bytesToRead = inBuffer->mAudioDataBytesCapacity;
int srcOffset = 0;
while (bytesToRead > 0) {
UInt32 maxBytesFromCurrentPiece = self.currentAudioPiece.audioData.length - self.currentAudioPieceIndex;
//Take the min of what the current piece can provide OR what is needed to be read
UInt32 bytesToReadNow = MIN(maxBytesFromCurrentPiece, bytesToRead);
NSData *subRange = [self.currentAudioPiece.audioData subdataWithRange:NSMakeRange(self.currentAudioPieceIndex, bytesToReadNow)];
//Copy what you can before continuing loop
void *srcStart = (&inBuffer->mAudioData + srcOffset);
memcpy(srcStart, subRange.bytes, subRange.length);
srcOffset += subRange.length;
bytesToRead -= bytesToReadNow;
Appcode shows no error but the compiler shows this:
"Semantic Issue" - Cannot initialize a variable of type 'void *' with
an rvalue of type 'void *const *'
On the line with "*srcStart".

Try changing
void *srcStart = (&inBuffer->mAudioData + srcOffset);
to
char *srcStart = ((char *)inBuffer->mAudioData) + srcOffset;
This will skip the appropriate number of bytes.

Related

How do I create a std::filesystem::exists() compatible path from Windows::Storage::StorageFile? [duplicate]

I'm trying to open a file using a string parameter, however I'm getting the following error:
error C2664: 'void std::basic_ifstream<_Elem,_Traits>::open(const wchar_t *,std::ios_base::openmode,int)' : cannot convert parameter 1 from 'System::String ^' to 'const wchar_t *'
How do you convert System::String ^ to const wchar_t *?
As Hans points out, simple conversion is necessary. It would look similar to the following:
System::String ^str = L"Blah Blah";
pin_ptr<const wchar_t> convertedValue = PtrToStringChars(str); // <-- #include <vcclr.h>
const wchar_t *constValue = convertedValue; // <-- Unnecessary, but to be completely verbose
void std::basic_ifstream<_Elem, _Traits>::open(constValue, mode, i);

error: incompatible types when initializing type 'char *' using type 'double'

Good afternoon,
I've got this code below:
double bd_sacar_imc(double altura, double peso) {
sqlite3 *db;
char *zErrMsg = 0;
int rc;
char *sql = ("SELECT %lf/(%lf * %lf);", peso, altura, altura);
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
return 0;
} else {
fprintf(stdout, "Consulta creada con exito\n");
return peso / (altura * altura);
}};
However, I get this error:
..\BD\bd.c:170:14: error: incompatible types when initializing type 'char *' using type 'double' char *sql = ("SELECT %lf /( %lf * %lf);", peso, altura, altura);
What might be happening?
It looks like you are trying to build a string. For that you can use snprintf.
char sql[64];
int size = snprintf(sql, 64, "SELECT %lf/(%lf * %lf);", peso, altura, altura);
Note that I kept the return value. This is to handle any future error where you modify a statement in a way that might result in buffer overflow or other error. You can probably just deal with this by using a debug assertion, as this pre-sized string should be large enough to hold the intended SQL.
if (size < 0 || size >= 64) {
assert(0);
return 0;
}
However, since you are using sqlite library, you should instead use the proper methods to prepare a statement and bind values to it, rather than inserting the values into the string yourself.

Using method_getReturnType to call specific types of instance member functions

I'm new to Objective-C so I don't have much idea about the language.
What I'm trying to do is go through all available instance methods of an object and call the ones that take no arguments, return bool and start with the string "func".
Here's how I get the methods:
uint32_t methodCount = 0;
Method * methods = class_copyMethodList(object_getClass(self), &methodCount);
I iterate through the methods and when the above condition matches, try to call them:
NSString * methodName = [NSString stringWithUTF8String:sel_getName(method_getName(method))];
char retTyp[10];
method_getReturnType(method, retTyp, 10);
const char * desiredRetType = "B";
if([methodName hasPrefix:#"func"] &&
(0 == strncmp(retTyp, desiredRetType, strlen(desiredRetType))) &&
(2 == method_getNumberOfArguments(method)))
{
bool * (* testMethod) (id, Method) = (void (*) (id, Method, ...)) method_invoke;
result = testMethod(self, method);
}
I had to experimentally figure out what the return type string is (turns out it's "B" for bool), and the number of arguments.
I'm getting the following error on the line where I'm trying to call the function using method_invoke:
cannot initialize a variable of type 'bool *(*)(__strong id, Method)' (aka 'bool *(*)(__strong id, objc_method *)') with an rvalue of type 'void (*)(__strong id, Method, ...)' (aka 'void (*)(__strong id, objc_method *, ...)'): different return type ('bool *' vs 'void')
Is there a better way to way to do this than class_copyMethodList?
How do I cast the function correctly so as to not get an error?
Is it possible that the method_getReturnType() conversion of return
types may change from system to system? Or is it always B for bool?
NVM, I figured it out. Instead of using method_invoke on the method name, I did this:
NSString * methodName = [NSString stringWithUTF8String:sel_getName(method_getName(method))];
char retTyp[10];
method_getReturnType(method, retTyp, 10);
const char * desiredRetType = "B";
if([methodName hasPrefix:#"func"] &&
(0 == strncmp(retTyp, desiredRetType, strlen(desiredRetType))) &&
(2 == method_getNumberOfArguments(method)))
{
SEL testMethod = method_getName(method);
return [self performSelector:testMethod];
}

How do I convert array<unsigned char> to an unsigned char[]?

In a CLR project I have the output of AesManaged class as a 16 byte array
array<unsigned char>^ result = msEncrypt->ToArray();
However I need to convert this to an array of type unsigned char defined like this
unsigned char buff[16];
EDIT: I did try this but its giving error (no method signature with those parameters, although there is one)
System::Runtime::InteropServices::Marshal::Copy(result, 0, buff, 16);
And this one
buff = reinterpret_cast<unsigned char>(&result);
But the error is Expression must be a modifiable lvalue
According to this MSDN documentation I used this and it appears to work
pin_ptr<unsigned char>buff = &result[0];

c and objective-c -- const char* and char*

I have a function:
-(void)ADPCMDecode:(char *)indata : (short *)outdata :(long)len {
indata is a char and the function does pointer arithmetic to iterate for a length of len, modifying outdata, which is a short and I will need to do pointer arithmetic to get the values from it.
I am attempting to call the function using:
const char *modulatedBytes1 = [modulatedAudio bytes];
char *modulatedBytes [] = modulatedBytes1;
unsigned int moduleatedLength = [modulatedAudio length];
short *decompressedBytes = NULL;
[self ADPCMDecode:modulatedBytes :decompressedBytes :moduleatedLength];
DLog(#"%hi",decompressedBytes[1]);
I get a BAD ACCESS error on this line: *outp++ = valprev; within the function, because I am passing a constant char * instead of a char *
How should I call the function, and how would I get the output from it?
I have no background in C, which is why I do not understand how to go about doing this.
Here is the C only version of the same question:
https://pastee.org/d3y3z