While reading Stroustrup's "The C++ programming language", I was puzzled at 'declaring a pointer to an array of character strings and declaring a reference to an array of 10 integers', and I had to clear my mind on pointers to arrays. I am posting here the notes I made to get corrections/suggestions etc, and hoping that C++ novices like myself might find it useful. In the code snippets below, I have declared various array variables along with declaration of corresponding pointers. If 'abc' is the name of an array variable, 'abc_ptr' is the name I have given to its pointer.
(a) Declaring a pointer to an array of 10 integers.
Concept: The name of an array is a constant pointer to the first array element, 'constant pointer' means it will constantly point to first array element.
int int_array[10]={1,2,3,4,5,6,7,8,9,0};
int_array is a pointer that constantly points to int_array[0], type of int_array is: int* const
int* const int_array_ptr1=int_array;//can't do int_array_ptr1++
int* int_array_ptr2=int_array;//can do int_array_ptr2++
for (int i=0; i<10; ++i )//prints all 10 integers stored in int_array[]
std::cout<<*(int_array_ptr2++)<<std::endl;
(b) Declare a pointer to an array of character strings.
char char_string_array[][6]={"alpha","beta","gamma"};
Here char_string_array is a constant pointer to first array element i.e char_string_array[0], type of char_string_array is: char (*const)[6]
char (*char_string_array_ptr)[6] =char_string_array;
Without the use of ( ) , char_string_array_ptr will be an array of 6 char* . Interpretation of above pointer declaration: char_string_array_ptr is a pointer to first element of char_string_array, this first element is an array of 6 chars.
std::cout<<sizeof(char_string_array_ptr)<<std::endl; //gives 4 bytes on my machine
for(int i=0; i<3; ++i)//prints all 3 strings stored in char_string_array[]
std::cout<<*(char_string_array_ptr++)<<std::endl;
(c) Pointer to an array of char*
char* char_string_array2[]={"one","two","three"};
char** char_string_array2_ptr=char_string_array2;
for(int i=0; i<3; ++i)//prints all 3 strings stored in char_string_array2[]
std::cout<<*(char_string_array2_ptr++)<<std::endl;
(d) Declaring a reference to an array of 10 integers.
int int_array[10]={1,2,3,4,5,6,7,8,9,0};
int (&ref_int_array)[10]=int_array;//without ( ) ref_int_array will be an array of 10 int&
for (int i=0; i<10; ++i)//prints all 10 integers stored in int_array[]
std::cout<<ref_int_array[i]<<std::endl;
(e) Pointer to a 2d float array.
float float2d_array[2][2]={0.1,0.2,1.0,2.0};//float2d_array is constant pointer to a float array , type of float2d_array is: float *const [2]
float (*float2d_array_ptr)[2]=float2d_array;
for(int i=0;i<2;++i){//prints all 4 values stored in float2d_array[]
for(int j=0;j<2;++j){
std::cout<<(*float2d_array_ptr)[j] <<std::endl;
}
++float2d_array_ptr;
}
(f) Pointer to a 3d array of ints
int int3d_array[2][2][2]={1,2,3,4,5,6,7,8};//int3d_array is constant pointer to a 2d array of ints, type of int3d_array is: int (*const) [2][2]
int (*int3d_array_ptr)[2][2]=int3d_array;
std::cout<< *(*(*(int3d_array_ptr+1)+1)+1)<<std::endl;//prints last element i.e 8
(g) Pointer to array of structures.
struct Name{
char* first_name;
char* last_name;
};
Name name_array[2]={ {"asim","khan"},{"rafay","ali"}};
Name* name_array_ptr=name_array;//pointer to a struct array
(h) Pointer to a 2d array of structures.
Name name_2darray[2][2]={ { {"asim","khan"}, {"rafay","ali"} },{ {"farheen", "farheen"}, {"kiran","M"} } };//type of name_2darray is: Name (*const name_2darray) [2]
printFirstNames(name_2darray,2);//prints first name from all 4 Names
void printFirstNames(Name (*const name_2darray) [2], const int size ) {
Name (*name_2darray_ptr)[2]=name_2darray;
for(int i=0;i<size;++i){
for(int j=0;j<size;++j){
std::cout<<(*name_2darray_ptr)[j].first_name<<std::endl;
}
++name_2darray_ptr;
}
}