• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Newbie Q about pointers and arrays

I'm a newb to C++ and like most newbs I'm struggling with the use of pointers a bit. Can someone check that I have the following code interpreted correctly?

CPeerGroup *g_cPeerGroup = NULL;

1. This line creates a pointer to a CPeerGroup object?

g_cPeerGroup = new CPeerGroup[g_iMaxNumGroups[PEERGROUP]];

2. This line allocates memory for an array of CPeerGroup objects and returns the pointer to the first element?

This is the part that's the crux of my confusion...

for (iCounter = 0; iCounter < g_iCurrentNumGroups[PEERGROUP]; ++iCounter)//DP 11/29 Changed to currentNumGroups
      {
            g_cPeerGroup[iCounter].ClearMemberList();
      }

3. g_cPeerGroup[X] is the address of the Xth CPeerGroup object?

4. You can call a member function of the CPeerGroup class with (address).ClearMemberList() without having to dereference the address (i.e. *g_cPeerGroup[iCounter].ClearMemberList())?

Can someone answer the four questions above? Thanks!

Dan
0
ltdanp22
Asked:
ltdanp22
2 Solutions
 
Infinity08Commented:
>> CPeerGroup *g_cPeerGroup = NULL;
>> 
>> 1. This line creates a pointer to a CPeerGroup object?

Right. It is initialized to NULL, to show that it's currently not pointing anywhere.


>> g_cPeerGroup = new CPeerGroup[g_iMaxNumGroups[PEERGROUP]];
>> 
>> 2. This line allocates memory for an array of CPeerGroup objects and returns the pointer to the first element?

Correct. The array will consist of g_iMaxNumGroups[PEERGROUP] objects.


>> for (iCounter = 0; iCounter < g_iCurrentNumGroups[PEERGROUP]; ++iCounter)//DP 11/29 Changed to currentNumGroups
>>       {
>>             g_cPeerGroup[iCounter].ClearMemberList();
>>       }
>> 
>> 3. g_cPeerGroup[X] is the address of the Xth CPeerGroup object?

g_cPeerGroup[X] is the (X+1)-th object in the array. It's not its address - it is the object itself.
Note that g_cPeerGroup[X] is the same as *(g_cPeerGroup + X), where (g_cPeerGroup + X) is the address of the (X+1)-th object in the array. And the dereference operator * dereferences that pointer to get to the object itself.


>> 4. You can call a member function of the CPeerGroup class with (address).ClearMemberList() without having to dereference the address (i.e. *g_cPeerGroup[iCounter].ClearMemberList())?

So, no. Due to the misunderstanding I explained in 3., this is not correct.
0
 
HooKooDooKuCommented:
An array variable is a pointer... a pointer to the 1st element in the array.
When you use [] with the array (i.e. pointer) you are dereferincing the pointer to a particular element of the array, similar to how * dereference a normal pointer.

int A[10]; //Array of 10 integers - A is basically a pointer to A[0]
int* pA = A; //Access to A via a pointer... pA points to A[0]
pA++; //pa points to A[1]
int B = A[1]; //Dereference the [1] element of A
int C = *pA; //C is not the same as B because we dereferenced a pointer that was pointing to A[1]
//So you can see that A[x] and *pA do SORT of the same thing.  The difference is that *pA accesses the data that pA points to, while A[x] accesses the data that is 'x' positions away from the pointer 'A'.
0
 
Infinity08Commented:
>> An array variable is a pointer...

No, that's incorrect. An array and a pointer are two completely different types. In certain contexts, an array transparently "reverts" to a pointer, but that doesn't mean you can treat it like a pointer.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
HooKooDooKuCommented:
Ok, lets get a little more precise.
"An array variable is a const pointer (the pointer is a const, not what it points to)."

In other words, the two following variables are basically the same thing, the only difference is that the array allocates a block of memory as well:
int A[10];
int* const pA = A;

As an example, the following is legal C code:

int A[10];        //defines the array A allocating a block of 10 integers
int* const pA = A; //defines a pointer, but the pointer can not change to point to another memory address
*pA = 10; //this is the same as the code A[0] = 10
*A = 10;  //dereferincing A as a pointer, changes the value at A[0]
int B = pA[2];  //I can even use the array dereferencing brackes with a pointer.

Now, exactly how is 'A' NOT a pointer again?  

Granted, the array variable ('A' in this example) can not be modified to point to anything other than the allocated block of array memory.  But in all other respects, the array VARIABLE ('A'') is indeed a pointer.
0
 
Infinity08Commented:
>> "An array variable is a const pointer (the pointer is a const, not what it points to)."

No, as I said earlier : pointers and arrays are two entirely different types.


>> the only difference is that the array allocates a block of memory as well:

There are many more differences than that. Type compatibility, allocation, behavior of sizeof, assignment, ...


>> Now, exactly how is 'A' NOT a pointer again?

Quite simply : because it's an array. Just because a table and a chair both have 4 legs doesn't mean they're the same.


>> But in all other respects, the array VARIABLE ('A'') is indeed a pointer.

Not true. As I said earlier : in some contexts, an array can transparently "revert" to a pointer, but that doesn't make it a pointer.

You don't seem to believe me, so let me give you a few examples :

1) What would sizeof(A) and sizeof(pA) give you ?

2) You might be able to assign an array to a pointer, but could you do the reverse ?

3) Could you choose the size of an array at runtime ? Would you be able to change that size ?

4) How about function arguments ?

5) What about the possible difference in memory location ?

and finally a question : if arrays are just const pointers, then why have them ? Why not just use const pointers ?
0
 
HooKooDooKuCommented:
>>1) What would sizeof(A) and sizeof(pA) give you ?
Now THAT is a very good point.  That is indeed a HUGE difference between pointers and arrays.
sizeof(pA) is going to return the size of a pointer.
But sizeof(A) is going to return the size of the data (i.e. the total size of all the array elements).
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> "An array variable is a const pointer (the pointer is a const, not what it points to)."

Just to back up what I8 has said...

Exceptions where arrays are not treated as a pointer
http://www.cplusplus.com/forum/articles/8/

The difference between pointers and arrays
http://www.cplusplus.com/forum/articles/9/

Array is not pointer
http://www.cplusplus.com/forum/articles/10/
0
 
ZzoniCommented:
Another way of looking at this common problem (misconception) is that when an array is defined, it is a compile time construct. The compiler takes care of generating code to use the array. When a pointer is defined, the compile generates code that allocates memory for the pointer. A pointer can hold a value (what is pointed to). Think of an array name as simply the address of the first element of the array. That's all the compiler needs to know (and encode) to accomplish what you want with arrays.

One subtlety is that WHEN an array is passed to a function memory for the array name is used on the stack (where all parameters are passed). In that case, you can argue, and use the function argument like a pointer since in this case the array now has run time storage allocated (on the stack).

Bottom line: arrays are manipulated (by the compiler) by using the array name as the address of the first element (index 0). So Array[2] is really the same as *(Array + 2) and the compiler takes care of types and their sizes (since 2 may not imply 2 bytes, Array could be floats, so the "offset" wouild be 2 *sizeof(float) from the Array base address.
0
 
Infinity08Commented:
>> WHEN an array is passed to a function

For clarification, technically, there is no way to pass an array to a function as parameter. What's passed instead is a pointer to the first element of the array. So, inside the function, all array semantics are lost (for example, sizeof would no longer give you the size of the array), as you're dealing with a pointer instead.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now