Objective c 重写指向输出参数的返回指针
我正在玩openSSL库,它需要我复习指针,我遇到了困难 我有一个objective-c方法:Objective c 重写指向输出参数的返回指针,objective-c,function,pointers,Objective C,Function,Pointers,我正在玩openSSL库,它需要我复习指针,我遇到了困难 我有一个objective-c方法: -(unsigned char *)encryptTake1:(unsigned char *)input inputLength:(int)inLen outputLength:(int*)outLen; 它获取一些数据,对其进行加密,并返回指向该数据的指针和数据长度作为输出参数 我想对此进行更改,以便将加密数据也作为输出参数处理,并使用返回值指示成功或失败。这就是我所拥有的: -(int)enc
-(unsigned char *)encryptTake1:(unsigned char *)input inputLength:(int)inLen outputLength:(int*)outLen;
它获取一些数据,对其进行加密,并返回指向该数据的指针和数据长度作为输出参数
我想对此进行更改,以便将加密数据也作为输出参数处理,并使用返回值指示成功或失败。这就是我所拥有的:
-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char *)output outputLength:(int*)outLen;
这不管用。我做错了什么?我认为问题在于
(unsigned char*)
是错误的。如果(unsigned char*)
是错误的,那么我认为我还需要更改方法中引用输出的方式。如何分配?这取决于您如何处理内存分配
-encryptake1:
返回什么?如果它返回调用者必须释放的新分配的缓冲区,那么您将在encryptTake2中使用unsigned char**
:
-(int)encryptTake2:(unsigned char *)input inputLength:(int)inLen output:(unsigned char **)outputPtr outputLength:(int*)outLen
{
*outputPtr = malloc(1024);
unsigned char* output = *outputPtr;
strcpy(output, "hello");
...
}
谢谢-就这样!我也对调用函数中的&
和*
感到困惑。在调用函数中,var被定义为无符号字符*
,并在函数调用中以&
作为前缀。在encryptTake2:
中,对var的所有引用都以*
作为前缀。我需要重读C语言书的第五章。