需要在c函數中生成一些隨機的10字節長度的字符串並從objective-c調用該函數。所以,我創建了一個指向uint8_t的指針並將它傳遞給C函數。該函數生成隨機字節並將它們分配給* randomString。但是,從函數返回到objective-c後,randomValue指針指向NULL。如何將生成的數組從c函數返回到objective-c
這裏是我的隨機函數在C:
void randomString(uint8_t *randomString)
{
randomString = malloc(10);
char randomByte;
char i;
for (i = 0; i < 10; i++) {
srand((unsigned)time(NULL));
randomByte = (rand() % 255) + 1;
*randomString = randomByte;
randomString++;
}
}
下面是Objective-C的部分:
uint8_t *randomValue = NULL;
randomString(randomValue); //randomValue points to 0x000000
NSString *randomString = [[NSString alloc] initWithBytes:randomValue length:10 encoding:NSASCIIStringEncoding];
NSLog(@"Random string: %@", randomString);
究竟是我在想什麼! :) – haroldcampbell