鍍金池/ 教程/ C/ Objective-C 數(shù)組的指針
Objective-C 多態(tài)性
Objective-C 預(yù)處理器
Objective-C for循環(huán)
Objective-C 開發(fā)環(huán)境(安裝配置)
Obj-C Foundation/基礎(chǔ)框架
Objective-C 指針運(yùn)算
Objective-C 循環(huán)
Objective-C 錯(cuò)誤處理
Objective-C while循環(huán)
Objective-C if語句
Objective-C do...while循環(huán)
Objective-C教程
Objective-C 嵌套switch語句
Objective-C 函數(shù)按值調(diào)用
Objective-C 常量
Objective-C 運(yùn)算符
Objective-C 數(shù)據(jù)類型
Objective-C 邏輯運(yùn)算符
Objective-C 數(shù)組作為函數(shù)參數(shù)傳遞
Objective-C struct/結(jié)構(gòu)
Objective-C 嵌套循環(huán)
Objective-C 函數(shù)引用調(diào)用
Objective-C 日志處理
Objective-C 數(shù)據(jù)存儲
Objective-C 教程首頁
Objective-C 擴(kuò)展
Objective-C 異常處理
Objective-C 類型轉(zhuǎn)換
Objective-C 嵌套 if 語句
Objective-C typedef
Objective-C 決策
Objective-C 指針的數(shù)組
Objective-C Protocols/協(xié)議
Objective-C 日期和時(shí)間
Objective-C 指針
Objective-C 內(nèi)存管理
命令行參數(shù)
Objective-C NSString/字符串
Objective-C 動態(tài)綁定
Objective-C 復(fù)合對象
Objective-C Arrays/數(shù)組
Objective-C 位運(yùn)算符
Objective-C 指向指針的指針
Objective-C 從函數(shù)返回?cái)?shù)組
Objective-C Posing/冒充
Objective-C Categories/類別
Objective-C 文件處理
Objective-C 賦值運(yùn)算符
Objective-C 函數(shù)返回指針
Objective-C if...else 語句
Objective-C switch語句
Objective-C URL加載系統(tǒng)
Objective-C 算術(shù)運(yùn)算符
Objective-C Numbers/數(shù)字
Objective-C語言程序結(jié)構(gòu)
Objective-C 快速枚舉
Objective-C 基本語法
Objective-C 類&對象
Objective-C 變量
Objective-C 關(guān)系運(yùn)算符
Objective-C 塊
Objective-C break語句
Objective-C continue語句
Objective-C 語言概述
Objective C 文本和字符串
Objective-C 函數(shù)
Objective-C 傳遞函數(shù)的指針
Objective-C 數(shù)組的指針
Objective-C 多維數(shù)組
Objective-C 繼承
Objective-C 數(shù)據(jù)封裝

Objective-C 數(shù)組的指針

在我們了解指針數(shù)組的概念,讓我們考慮下面的例子,這使得使用3個(gè)整數(shù)的數(shù)組:

#import <Foundation/Foundation.h>
 
const int MAX = 3;
 
int main ()
{
   int  var[] = {10, 100, 200};
   int i;
 
   for (i = 0; i < MAX; i++)
   {
      NSLog(@"Value of var[%d] = %d
", i, var[i] );
   }
   return 0;
}

上面的代碼編譯和執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

2013-09-14 00:22:57.136 demo[18206] Value of var[0] = 10
2013-09-14 00:22:57.136 demo[18206] Value of var[1] = 100
2013-09-14 00:22:57.136 demo[18206] Value of var[2] = 200

可能有情況時(shí),我們要保持一個(gè)數(shù)組,它可以存儲指針int或char或任何其他數(shù)據(jù)類型可用。以下是一個(gè)整數(shù)指針數(shù)組的聲明:

int *ptr[MAX];

此聲明指針數(shù)組的最大整數(shù)指針。因此,每一個(gè)元素的指針,現(xiàn)在持有一個(gè)int值的指針。下面的示例使用三個(gè)整數(shù),將被存儲在一個(gè)數(shù)組中的指針如下:

#import <Foundation/Foundation.h>
 
const int MAX = 3;
 
int main ()
{
   int  var[] = {10, 100, 200};
   int i, *ptr[MAX];
 
   for ( i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; /* assign the address of integer. */
   }
   for ( i = 0; i < MAX; i++)
   {
      NSLog(@"Value of var[%d] = %d
", i, *ptr[i] );
   }
   return 0;
}

上面的代碼編譯和執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

2013-09-14 00:27:47.904 demo[3825] Value of var[0] = 10
2013-09-14 00:27:47.905 demo[3825] Value of var[1] = 100
2013-09-14 00:27:47.905 demo[3825] Value of var[2] = 200

也可以使用一個(gè)指針數(shù)組,字符存儲的字符串列表如下:

#import <Foundation/Foundation.h>
 
const int MAX = 4;
 
int main ()
{
   char *names[] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
                   };
   int i = 0;

   for ( i = 0; i < MAX; i++)
   {
      NSLog(@"Value of names[%d] = %s
", i, names[i] );
   }
   return 0;
}

上面的代碼編譯和執(zhí)行時(shí),它會產(chǎn)生以下結(jié)果:

2013-09-14 00:30:25.438 demo[13476] Value of names[0] = Zara Ali
2013-09-14 00:30:25.439 demo[13476] Value of names[1] = Hina Ali
2013-09-14 00:30:25.439 demo[13476] Value of names[2] = Nuha Ali
2013-09-14 00:30:25.439 demo[13476] Value of names[3] = Sara Ali