Question: Traverse the arraylist using pointer offset, point arithmetic and -> operator. I have already defined the functions for you, you have to complete the for
Traverse the arraylist using pointer offset, point arithmetic and -> operator. I have already defined the functions for you, you have to complete the for loop. Each function takes the array list and the count of items. You have to traverse the list and print the items using the pointers.
#include
#include
struct _person {
int age;
char name[32] ;
} ;
// In this function, you have to use the pointer offset
// to traverse the array and print.
void print_names_offset( struct _person *pList, int cnt)
{
int i = 0;
for ( i = 0 ; i < cnt ; i++) {
// print the name using the pointer offset concept ....ptr+i
;
}
}
// In this function, you have to use the pointer arithmetic
// to traverse the array and print.
void print_names_arithmetic( struct _person *pList, int cnt)
{
int i = 0;
for ( i = 0 ; i < cnt ; i++ ) {
// print the name using the pointer arithmetic concept ....ptr++
;
}
}
//Use -> operator to traverse the list and print the names
void print_names_arrowOperator ( struct _person *pList, int cnt)
{
int i = 0;
for ( i = 0 ; i < cnt ; i++ ) {
// print the name using the -> notation
;
}
}
int main ( )
{
struct _person person ;
struct _person pArray[2] ;
pArray[0].age = 10 ;
strcpy (pArray[0].name, "John") ;
pArray[1].age = 40 ;
strcpy (pArray[1].name, "Maggie") ;
print_names_offset ( pArray, 2 ) ;
print_names_arithmetic ( pArray, 2 ) ;
print_names_arrowOperator ( pArray, 2 ) ;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
