Hi,

I'm playing around with pointers and I found out something.
For example, I have the array of objects of structure, and wished to point to it over the pointer which already points to it ( pointer to pointer to array of objects. ).
Confusing? Code:

struct person{int number;};
int main()
{
    person object[3];
    for(int j=0;j<4;j++){                        //Just filling variable number
            object[j].number=j+1;
            cout<<(object[j].number=j+1)<<endl;
            }
            
    person *pobject;       //Declaring pointer to object
    pobject=object;
     
     for(int i=0;i<4;i++){                   //Checking how does it point (good/bad)
                  cout<<(pobject[i].number)<<endl;   
                     }
          
     
     person **pobject1[3]; //Declaring new pointer
     pobject1[0]=&pobject; 
     
     for(int j=0;j<=5;j++){     //Printing variable number 
             for(int i=0;i<=5;i++){
                  cout<<(pobject1[i][j]->number)<<endl;   
                  }
             }
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

I wished to create the array of objects of structure which will have variable number defined and create pointer to it ( which works great ), and afterwards create new pointer which'd point to existing one.

I realized if I pointed by this way:

pobject[0]=&pobject;

I'd have to use 2D array to represent variable number iterating by pointer.

I have no idea how does it work.
It just prints first variable number from first object and crashes.

Any help?
Thanks.

> I have the array of objects of structure, and wished to point to it over the pointer
> which already points to it ( pointer to pointer to array of objects. ).
> Confusing?

Using a few typedefs would help eliminate the confusion:

int main()
{
   struct person{ int number ; } ;
   enum { N = 3 } ;

   typedef person array_type[N] ;
   typedef array_type* pointer_to_array_type ;
   typedef pointer_to_array_type* pointer_to_pointer_type ;


   array_type a = { person{ 10 }, person{ 111 }, person{ 1222 } } ;
   pointer_to_array_type p = &a ;
   pointer_to_pointer_type pp = &p ;

   std::cout << a[0].number << ' '
             << (*p)[1].number << ' '
             << (**pp)[2].number << '\n' ;
}
typedef person array_type[N] ;
      typedef array_type* pointer_to_array_type ;
      typedef pointer_to_array_type* pointer_to_pointer_type ;

1st line defines array_type[N] as a data type which creates array of objects of person structure?
2nd line defines data type pointer_to_array_type which will create pointers to objects of structure person?
3rd line defines data type pointer_to_pointer_type which will create pointers to pointers to objects of structure person?

> 1st line defines array_type[N] as a data type which creates array of objects of person structure?
array_type is the type, it is an alias for an array of N persons.

> 2nd line defines data type pointer_to_array_type which will create pointers to objects of structure person?
No. The type pointer_to_array_type is a pointer to an array of N persons.

> 3rd line defines data type pointer_to_pointer_type which will create pointers to pointers to objects of structure person?
No. The type pointer_to_pointer_type is a pointer to a pointer_to_array_type

Try this: (demangling is g++ specific)

#include <cxxabi.h>
#include <typeinfo>
#include <iostream>

template< typename T > void print_demangled_type_name()
{
    enum { BUFFSZ = 2048 } ;
    char temp[ BUFFSZ ] ;
    std::size_t sz = BUFFSZ ;
    int status ;
    __cxxabiv1::__cxa_demangle( typeid(T).name(), temp, &sz, &status ) ;
    std::cout << ( status==0 ? temp : "__cxa_demangle error" ) << '\n' ;
}

template< typename T > void print_demangled_type_name( const T& t )
{  print_demangled_type_name<T>() ; }


struct person{ int number ; } ;

int main()
{
   enum { N = 3 } ;
   typedef person array_type[N] ;

   array_type a ;
   person b[N] ; // b is of the same type as a
   print_demangled_type_name<array_type>() ; // prints: person[3]
   print_demangled_type_name(a) ; // prints: person[3]
   print_demangled_type_name(b) ; // prints: person[3]

   typedef array_type* pointer_to_array_type ;
   pointer_to_array_type c = &a ;
   person (*d)[N] = &b ; // d is of the same type as c
   print_demangled_type_name<pointer_to_array_type>() ; // prints: person (*) [3]
   print_demangled_type_name(c) ; // prints: person (*) [3]
   print_demangled_type_name(d) ; // prints: person (*) [3]

   typedef pointer_to_array_type* pointer_to_pointer_type ;
   pointer_to_pointer_type e = &c ;
   person (**f)[N] = &d ; // f is of the same type as e
   print_demangled_type_name<pointer_to_pointer_type>() ; // prints: person (**) [3]
   print_demangled_type_name(e) ; // prints: person (**) [3]
   print_demangled_type_name(e) ; // prints: person (**) [3]
}

If you could explain why you think you need a pointer to a pointer to an array (what is the programming problem that you are faced with), someone would be able to point out a far simpler way of solving it.

Why do you need a pointer to an array in the first place? After that we can come to why a pointer to a pointer to an array.

If you could explain why you think you need a pointer to a pointer to an array (what is the programming problem that you are faced with), someone would be able to point out a far simpler way of solving it.

Why do you need a pointer to an array in the first place? After that we can come to why a pointer to a pointer to an array.

There's no concrete reason, I just wanted to research about pointers.
Thanks for help.

This article has been dead for over six months. Start a new discussion instead.