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;
    }
}

Recommended Answers

All 4 Replies

I am sorry to say that this entire long message is based on a fundamental misconception.

The name of an array is not a pointer. Its type is an array type, not a pointer type. Array types and pointer types are different from each other.

You can see this difference in several contexts. For example:

int x[10];
std::cout << sizeof(x) << std::endl;

If x were a pointer, then sizeof(x) would be the size of a pointer; but if you try running this code, you will see that it isn't.

As another example, consider

int arr[5];
int *const *const p = &arr;

Again, if arr were a pointer, this code would compile, but it doesn't.

What is true is that if you use the name of an array in most contexts (the main exceptions being to bind a reference to it or to use it as an operand of unary & or sizeof), it is automatically converted to a pointer to its initial element.

Thanks arkoenig for pointing out the mistake. I should have been explicit in mentioning that I find the mentioned concept(fairy tale) useful when writing declaration for pointer to array. It is a thought facilitator for me and mostly works, but may not be exactly true. I wanted to edit and correct the post but can't figure out how.

To be correct we can say : The name of an array can be used as a pointer to its initial element.

int x[2]={1,0};
char a;
a=x;//Compilation gives an error: Invalid conversion from int* to char
//g++ sees the type of x as int*

>> wanted to edit and correct the post but can't figure out how.

You only have 30 minutes to make changes. After that you are just SOL ("Sadly Outta Luck" (polite form) for anyone who doesn't know that that means).

Your code snippet is in error. char a; should be char* a;

>>
Your code snippet is in error. char a; should be char* a;

The error is self induced. Only to show that g++ is seeing array name "x" as an int* [compiler error message: Invalid conversion from int* to char] in this context.

Besides, char a; should not be char* a; if you want to remove error from this code snippet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.