2017-07-19 107 views
0
#import <Foundation/Foundation.h> 

/* function to generate and return random numbers. */ 
int * getRandom() 
{ 
    static int r[10]; 
    int i; 

    /* set the seed */ 
    srand((unsigned)time(NULL)); 
    for (i = 0; i < 10; ++i) 
    { 
     r[i] = rand(); 
     NSLog(@"%d\n", r[i]); 
    } 
    NSLog(@"checking for r value: %d\n", r); 
    return r; 
} 

/* main function to call above defined function */ 
int main() 
{ 
    /* a pointer to an int */ 
    int *p; 
    int i; 

    p = getRandom(); 
    for (i = 0; i < 10; i++) 
    { 
     NSLog(@"*(p + [%d]) : %d\n", i, *(p + i)); 
    }`enter code here` 

    return 0; 
} 

請問我需要一個關於objective-c的新東西,關於運行上面的代碼(「檢查r值:6296000」)成爲輸出的「NSLog(@」檢查r值:%d \ n「,r)」,r變成了6296000,請問6296000的含義是什麼?目標C中的指針返回函數的指針

+0

r是一個數組。另請參見[this](https://stackoverflow.com/questions/3959705/arrays-are-pointers)問題。 –

回答

0

您正在打印指針r(ints數組中的第一個指針)。

要使用r [position]打印數組中位置的值。

6296000是指向數組r的指針地址的基本10版本。

+0

Wao !! silassales你的回答是非常有幫助的 –