Array of Pointer - Objective C
Before we understand the concept of array of pointer , let us consider the following example ,which makes use of an array of 3 intergers.
There may be situation when we want to maintain an array, which can store pointers to an int or char or any other data type available.Following is the declaration of an array of pointers to an integer:
This declares ptr as an array of MAX integer pointer. Thus each element in ptr now holds a pointer to an int value. Following example makes use of three integers,which will be stored in an array of pointer as follows:
You can also use an array of pointers to character to store a list of string as follows:
i hope understand more and more better way plz comment and share
#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\n", i, var[i]);
}
return 0;
}
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\n", i, var[i]);
}
return 0;
}
There may be situation when we want to maintain an array, which can store pointers to an int or char or any other data type available.Following is the declaration of an array of pointers to an integer:
int *ptr[MAX];
This declares ptr as an array of MAX integer pointer. Thus each element in ptr now holds a pointer to an int value. Following example makes use of three integers,which will be stored in an array of pointer as follows:
#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 an integer
}
for (i =0; i< MAX; i++)
{
NSLog(@"value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
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 an integer
}
for (i =0; i< MAX; i++)
{
NSLog(@"value of var[%d] = %d\n", i, *ptr[i]);
}
return 0;
}
You can also use an array of pointers to character to store a list of string as follows:
#import <Foundation/Foundation.h>
const int MAX = 4;
int main()
{
char *var[] = {"Nanon", "nahuds", "lokes", "lisa"};
int i ;
for (i =0; i< MAX; i++)
{
NSLog(@"value of var[%d] = %s\n", i, var[i]);
}
return 0;
}
const int MAX = 4;
int main()
{
char *var[] = {"Nanon", "nahuds", "lokes", "lisa"};
int i ;
for (i =0; i< MAX; i++)
{
NSLog(@"value of var[%d] = %s\n", i, var[i]);
}
return 0;
}
i hope understand more and more better way plz comment and share
No comments: