How are they different in C++?

1
2
// I presume Obj a[] is a pointer here? Am I correct?
void func1(Obj a[ ], unsigned n) 


vs
1
2
// Over here Obj* a[ ] is an array that points to type Obj? Am I correct?
void func2(Obj* a[ ], unsigned n)


 
const Obj* o = (const Obj*) o1;


vs
1
2
//all I know is that this is a pointer to at pointer
const Obj** o = (const Obj**) o1; 


Which ones are better vs the other and why. There are so many variations :(..
void func1(Obj a[ ], unsigned n) A is an array of Obj. It is a const pointer.

void func2(Obj* a[ ], unsigned n) a[] is an array of pointers to Objs.

Graham Wrote: "The code int array[3]; is the same as int const *array=new int[3];"



const Obj* o = (const Obj*) o1;
What is o1?. This is casting o1 to a constant pointer to a type of Obj.

const Obj** o = (const Obj**) o1; Same as above, but it's a pointer to a pointer.

It's not so much which one is better, but which one fits the purpose you are after. Double-Pointers are often used for 2D Arrays.

e.g
1
2
3
int **pGrid = new int*[10];
for (int i = 0; i < 10; ++i)
 pGrid[i] = new int[100];


We not have a 10x100 2D Array (or a Grid).

Have a look at: http://en.wikipedia.org/wiki/Const_correctness

Personally, I don't ever use consts. I don't see any real need with proper encapsulation. I am an OO-Junkie

@Zaita,

I also read on wikipedia that double indirection (**) is higher number of pointer dereferences, it will come at a performance penalty. Am I right in assuming that this would be a compelling reason to not use ** when you have a choice of using only *?

Yes. But performance loss is only going to be very minimal (maybe 1 instruction). Nothing to be overly concerned about.

But no use using a double pointer when you can get away with a single one. They have different purposes.

Pointers are something you will understand more when you actually use them in real-world situations when they are required. :)
@Zaita,

To take things further and bring up a "real world" example, if you will, the following is something I looked up.

1
2
3
4
5
6
7
8
9
void Foo1(Foo a[ ], unsigned n) {
    int i;
    char buffer[200];
    for (i = 0; i < n; i++)
  {
        to_string(&a[i], buffer);
        printf("%s\n", buffer);
   }  
}


vs

1
2
3
4
5
6
7
8
9
10
11
void Foo2(Foo* a[ ], unsigned n) 
{
   int i;
   char buffer[200];
   for (i = 0; i < n; i++) 
  {
   to_string(a[i], buffer);
   printf("%s\n", buffer);
  }
 }


From the above, the first points to a const array, that can be changed and as per http://en.wikipedia.org/wiki/Const_correctness, can be used to change the contents of a[] (in the first example), but in the second you cannot change the contents, only get the items from them. I also say this because this line of code:

 
to_string(&a[i], buffer); //from the first 


 
to_string(a[i], buffer); //from the second 


is also a distinguishing point.
Ok. You have confused yourself here.

An array is a constant pointer (in that the pointer cannot be changed to point at something else). Not a pointer to a constant object (in that the object's value cannot be changed).

to_string() requires that you pass a pointer to a Foo class. The first example our array stores objects of type 'Foo', while in the second the array stores pointers to objects of type 'Foo'.

So the & is used to pass the address of the object 'Foo' in the first, but in the second it's not needed because the array is of addresses already.
Last edited on
@Zaita,

I may have said this before...but you rock!!!

Thanks

No worries man :)
Topic archived. No new replies allowed.