Hello all,
I am solving some of the exercises in a new book of C++

I am having trouble in declaring

A Reference to a array of 10 Integers

Its on the 19TH and 30th line.
I have the total code over here.

//Declarations for all the types.

#include <iostream>

int main()
{
    /*Ignore all the following variables*/
    int b;
    char a;
    char c;
    char* d=&c;
    char characterstrings[]="Hello, World\n";
    /* Donot Ignore after this
    All the above are dummy variables required for Initialisation.
    The "//" Comments are the questions
    */
   char* sk;                        //A pointer to a character
   int array[10];                   //An Array of 10 integers array[0]....array[9]
   int& ref=array[0];/*Doubt*/      //A Reference to an array of 10 Integers.
   char* ptr_charstrings=characterstrings;//A pointer to an array of character strings.
   char** ptr_to_ptr;                //Pointer to a Pointer to char.
   const int ic=5;                     //Constant Integer.
   const int* ic_ptr;                 //A pointer to a constant integer.
   int const* constptr=&b;              //A constant pointer to an integer.
   /*------------------------------------------------------------------------------------------------*/
   /*------------------------------INITIALISATIONS---------------------------------------------------*/

   char* sk2=&a;
   int array2[10]={1,2,3,4,5,6,7,8,9,10};
   //int& ref2 (No Idea on what to do.)
   //Already Initialised Characterstrings
   char** ptr_to_ptr2=&d;
   //Constant integer already Initialised.
   const int* ic_ptr2=&b;
   //Constant Pointer already Intialisized.
}

I am not pretty sure that all of the above are correct so please help me correct them up.

It's not very intuitive (to me anyway), but a reference to an array is declared:
int array[10];
int (&ref)[10] = array;

It's not very intuitive (to me anyway), but a reference to an array is declared:
int array[10];
int (&ref)[10] = array;

Well i am really unable to understand .
I already have seen that possibility and thought that it might be an array of references to an int array.

Follow the right-left rule for declarations and you get:
(&ref) - ref is a reference
(&ref)[10] - to an array
int (&ref)[10] - of integers.

Not an array of references, which are not permitted I believe.

commented: Precisely +4

Thanks, I got it at last.
Although i was wondering whether all the others are correct too.

Thanks, I got it at last.
Although i was wondering whether all the others are correct too.

Give 'em an inch...;)

char characterstrings[]="Hello, World\n";
char* sk; //A pointer to a character :)
int array[10]; //An Array of 10 integers array[0]....array[9]
int& ref=array[0];/*Doubt*/ //A Reference to an array of 10 integers. :(
char* ptr_charstrings=characterstrings;//A pointer to an array of character strings. :(
char** ptr_to_ptr; //Pointer to a Pointer to char. :)
const int ic=5; //Constant Integer. :)
const int* ic_ptr; //A pointer to a constant integer.:)
int const* constptr=&b; //A constant pointer to an integer. :(

char* sk;                        //A pointer to a character
   int array[10];                   //An Array of 10 integers array[0]....array[9]
   int (&ref)[10]=array;      //A Reference to an array of 10 Integers.
   char *(ptr_charstrings)[10];//A pointer to an array of character strings.
   char** ptr_to_ptr;                //Pointer to a Pointer to char.
   const int ic=5;                     //Constant Integer.
   const int* ic_ptr;                 //A pointer to a constant integer.
   int const* constptr=&b;              //A constant pointer to an integer.

Well i guess all of them are now corrected. Thanks to http://www.codeproject.com/KB/cpp/complex_declarations.aspx

char* sk;                        //A pointer to a character
   int array[10];                   //An Array of 10 integers array[0]....array[9]
   int (&ref)[10]=array;      //A Reference to an array of 10 Integers.
   char *(ptr_charstrings)[10];//A pointer to an array of character strings.
   char** ptr_to_ptr;                //Pointer to a Pointer to char.
   const int ic=5;                     //Constant Integer.
   const int* ic_ptr;                 //A pointer to a constant integer.
   int const* constptr=&b;              //A constant pointer to an integer.

Well i guess all of them are now corrected. Thanks to http://www.codeproject.com/KB/cpp/complex_declarations.aspx

Well I'm not completely happy:

int const* constptr=&b;

is a pointer to a constant integer, NOT a constant pointer to an integer (it's interchangeable with const int* ic_ptr; )

commented: Thank You +3

Well I'm not completely happy:

int const* constptr=&b;

is a pointer to a constant integer, NOT a constant pointer to an integer (it's interchangeable with const int* ic_ptr; )

char* sk;                        //A pointer to a character
   int array[10];                   //An Array of 10 integers array[0]....array[9]
   int (&ref)[10]=array;      //A Reference to an array of 10 Integers.
   char *(ptr_charstrings)[10];//A pointer to an array of character strings.
   char** ptr_to_ptr;                //Pointer to a Pointer to char.
   const int ic=5;                     //Constant Integer.
   const int* ic_ptr;                 //A pointer to a constant integer.
   int *const constptr=&b;              //A constant pointer to an integer.

Well i am sorry for that.

Secondly
Are the typedefs correct?

#include <iostream>

int main()
{
    typedef unsigned char usc;              //Unsigned Char
    typedef const unsigned char cusc;       //Constant Unsigned Char
    typedef int* int_ptr;                   //pointer to int
    typedef char** ptr_ptr_char;            //pointer to pointer to char
    typedef char *mychar[];                 //pointer to an array of char
    typedef int *intarry[7];                //array of 7 pointers to int
    typedef intarry* ptr_ptr_array;         //pointer to an array of 7pointers to int
    typedef intarry* last[8];               //array of 8 arrays of 7 pointers to int.
}

typedef unsigned char usc; //Unsigned Char :)
typedef const unsigned char cusc; //Constant Unsigned Char :)
typedef int* int_ptr; //pointer to int :)
typedef char** ptr_ptr_char; //pointer to pointer to char :)
typedef char *mychar[]; //pointer to an array of char :(
typedef int *intarry[7]; //array of 7 pointers to int :)
typedef intarry* ptr_ptr_array; //pointer to an array of 7pointers to int :)
typedef intarry* last[8]; //array of 8 arrays of 7 pointers to int. :(

The declaration syntax is probably the biggest wart C++ has. Here are a few tricks for figuring out what a declaration means, or how to write one:

  1. Simple declarations can be read backward to get the real meaning: int *p; : p is a pointer to int int **p; : p is a pointer to a pointer to int int const * const *p; : p is a pointer to a const pointer to const int

    const can be swapped with the base type without changing the meaning of the declaration: const int * const *p; : p is a pointer to const pointer to const int or p is a pointer to const pointer to an int that's const, if you want to follow the syntax closely.

  2. Direct array and function suffixes are read first then the simple declaration rules apply: int *a[10]; : a is an array of ten pointers to int int const * const *f(); : f is a function that returns a pointer to const pointer to const int.
  3. Parentheses change the evaluation order such that everything in the parentheses is evaluated first: int (*f)(); : f is a pointer to a function that returns an int. int (*a)[10]; : a is a pointer to an array of ten int.
  4. Function parameters each follow all of these rules: void f ( const int ** const (*a)[10] ); : f is a function that takes one parameter called "a" which is a pointer to an array of ten const pointers to pointers to const int and returns nothing.
  5. Don't forget that a function can return a pointer (this is where it gets really confusing, but the rules remain the same): void (*f())(); : f is a function that returns a pointer to a function that returns void. int (*f())[10]; : f is a function that returns a pointer to an array of ten int.

If you combine all of this together, you can read pretty much any declaration with only a little trouble, or you can come up with some truly messed up code:

#include <iostream>
#include <string>

using namespace std;

template <int N>
class Foo {
    std::string base[N];
protected:
    std::string (*baz ( const char *prefix ))[N]
    {
        for ( int i = 0; i < N; i++ )
            base[i] = prefix + base[i];

        return &base;
    }
};

template <int N>
class Bar: public Foo<N> {
public:
    std::string (*(Bar::*pub_baz()) ( const char* ))[N]
    {
        return &Bar::baz;
    }
};

int main()
{
    const int n = 5;

    Bar<n> tipsy;

    for ( int i = 0; i < 3; i++ ) {
        std::string (*temp)[n] = ( tipsy.*tipsy.pub_baz() ) ( "pre" );

        for ( int i = 0; i < n; i++ )
            std::cout<< (*temp)[i] <<' ';

        std::cout<<'\n';
    }
}
#include <iostream>

int main()
{
    typedef unsigned char usc;              //Unsigned Char
    typedef const unsigned char cusc;       //Constant Unsigned Char
    typedef int* int_ptr;                   //pointer to int
    typedef char** ptr_ptr_char;            //pointer to pointer to char
    typedef char (*mychar)[];                 //pointer to an array of char
    typedef int *intarry[7];                //array of 7 pointers to int
    typedef intarry* ptr_ptr_array;         //pointer to an array of 7pointers to int
    typedef intarry (*last)[8];               //array of 8 arrays of 7 pointers to int.
}

Thanks for the post there Narue . And i think i have written it correctly now

Ooh, so close. last itself is a pointer type, so calling it an array is incorrect. I suspect you meant to say "pointer to array" but got ahead of yourself.

#include <iostream>

int main()
{
    typedef unsigned char usc;              //Unsigned Char
    typedef const unsigned char cusc;       //Constant Unsigned Char
    typedef int* int_ptr;                   //pointer to int
    typedef char** ptr_ptr_char;            //pointer to pointer to char
    typedef char (*mychar)[];                 //pointer to an array of char
    typedef int *intarry[7];                //array of 7 pointers to int
    typedef intarry* ptr_ptr_array;         //pointer to an array of 7pointers to int
    typedef intarry last[8];               //array of 8 arrays of 7 pointers to int.
}

How about this one :)

#include <iostream>

int main()
{
    typedef unsigned char usc;              //Unsigned Char
    typedef const unsigned char cusc;       //Constant Unsigned Char
    typedef int* int_ptr;                   //pointer to int
    typedef char** ptr_ptr_char;            //pointer to pointer to char
    typedef char (*mychar)[];                 //pointer to an array of char
    typedef int *intarry[7];                //array of 7 pointers to int
    typedef intarry* ptr_ptr_array;         //pointer to an array of 7pointers to int
    typedef intarry last[8];               //array of 8 arrays of 7 pointers to int.
}

How about this one :)

Got it, and full marks for perseverance!

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.