Archived

This topic is now archived and is closed to further replies.

What is the difference between a pointer and an array variable.

This topic is 5712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Let''s say I have two char arrays:
  
char originalArray[256];
char copyArray[256];
  
Let''s say that I want to make the copyArray equal to the originalArray. The thing is I don''t want to use strcpy() because I want changes in the originalArray to be reflected in the copyArray. I realize that I could make copyArray a pointer to the originalArray and do this:
  
char originalArray[256];
char* copyArray[256];
copyArray = &originalArray;
  
But other parts of my code depend on the copyArray being a regular array, and not a copy to an array (since in certain areas, I want to simply do a strcpy to it). My question is this. I know this doesn''t work, but could someone explain to me why:
  

char originalArray[256];
char copyArray[256];

copyArray = originalArray;
  
I figure that this should make the copyArray (or the address of the first element in it) equal to the first element of the original array. I apologize for this, since I expect the answer is rather basic, but we never covered it in any of my classes, or any of the books I''ve gone over. --Vic--

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

In your second section:
char originalArray[256];
char* copyArray[256];
copyArray = &originalArray;

You would actually want to do:
char originalArray[256];
char* copyArray;
copyArray = originalArray;

Your orignal suggestion creates in copyArray 256 char*''s, not a char* for a 256 array.

Anyway that change will take care of everything. You can treat copyArray as if it is originalArray. Just be careful that originalArray doesn''t go out of scope. That is that if original array is declared in a function and then the function ends the memory allocated for originalArray will be deallocated and copyArray will point to something else altogether. To avoid this the best way would be to do:

char* originalArray;
char* copyArray;
originalArray = new char[256];
copyArray = originalArray;

Then when you are done with both do:

delete[] originalArray;

Doing either of these will make copyArray and originalArray the exact same array, just with two different handles.

Share this post


Link to post
Share on other sites
The short answer is that array names are CONSTANT (read-only) pointers, while "true" pointers are just variables. They both point to memory, just that one of them is "hard coded."

The long answer is...

1) You declared two arrays of 256 chars, thus 512 bytes in all. Neither of them can be used as pointers because array names cannot be modified ("copyArray = originalarray;" would be illegal). If you wanted to make them equal, you would have to use memcpy() but this is not what you want to achieve, given your description.

2) You define the pointer incorrectly. The way you have done it, copyArray is an array of 256 pointers! You need to remove the [256] from the pointer definition. Also, since array names are just const pointers, you don''t put a & in front to get their address (since their name is an address already).

3) As I said earlier, array names (of which copyArray is one in this example) are CONST and cannot be modified, so your line of "copyArray = originalArray;" is illegal.

Think about it. You have an array 256 bytes long, with a constant variable assigned to its first element. The only way your program knows where this array is, is by the array name. If you were allowed to change copyArray, then nobody would know where those 256 bytes were anymore. You''d have a "memory leak," which means you have memory floating around your program that has become "lost" - nobody can access it anymore.


Hope this answers some of your questions.

Share this post


Link to post
Share on other sites
One more thing.. DO NOT USE STRCPY() TO COPY REGULAR ARRAYS!!! strcpy() ends when it hits a zero because it''s made to copy strings that end with a zero!

Share this post


Link to post
Share on other sites
I've messed around and I think I have a better way to describe what I'm looking for.

Why is this not legal?


      

char myArray[256];
void* myPointer;
// assuming that myPointer has a valid address

myArray = myPointer;



The compiler says that it can't cast from a pointer to an array type. I was wondering the differences between the two types (pointer and array).

--Vic--

OOPS! I guess the answer was posted while I was writing this. Foofightr, you answered everything I needed to know. I was guessing the answer had to do with the only way to access (and delete) the array would be by it's address. Changing it would give you a memory leak, like you said. Thanks a bunch.

[edited by - Roof Top Pew Wee on May 29, 2002 4:53:45 PM]

Share this post


Link to post
Share on other sites
btw, Im waiting for someone to say "just use < vector >". I give it a couple hours

[edited by - foofightr on May 29, 2002 4:55:03 PM]

Share this post


Link to post
Share on other sites
no one should tell you what to use. you will need to learn the concepts sometime....


char array[256]; // "array" is a constant pointer, and it's value cannot be modified, although you can modify the array elements it's pointing to....

char *ptr; // "ptr" is a pointer, and it's value can be modified, as well as you can modify the array elements it's pointing to....


ptr = array; // is a legal statement.

array = ptr; // is an illegal statement.


edit: to put it plainly, "array" cannot be used as an l-value.


To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

[edited by - jenova on May 29, 2002 5:18:17 PM]

Share this post


Link to post
Share on other sites
How's this foofightr?

Just use < vector >!

Hehe!

/*=========================================*/
/* Chem0sh */
/* Lead Software Engineer & Tech Support */
/* http://www.eFaces.biz */
/*=========================================*/

[edited by - Chem0sh on May 29, 2002 10:56:41 PM]

Share this post


Link to post
Share on other sites
It''s a simple C++ syntax rule and there''s nothing magical about it. Just remember that an array name can''t be an l-value and you will stay out of trouble.

Share this post


Link to post
Share on other sites