Advertisement

so whats the deal with pointers and assignment?

Started by November 19, 2002 04:17 PM
25 comments, last by TwistedMatrix 21 years, 11 months ago
quote: Original post by smart_idiot
Pointers and arrays are virtually the same thing.

No they''re not!
quote:
Alas, pointers and arrays are not 100% the same. (They were in C, but the introduction of classes and destructors screwed us.)

Pointers and arrays are *not* the same thing in C either - they''re exactly the same things as in C++. What makes you think classes and destructors change what they are? An array is a contiguous region of storage sufficiently big to contain n elements of a single type, whereas a pointer is just that: a pointer.
Yeah, that is a common misconception of a lot of people who are starting to program, though I can't really see why. Pointers and arrays aren't really related at all -- except superficially. An array is just a "row" of a bunch of data of the same type. The only relationship between arrays and pointers is that when you use the name of an array in an expression, it represents a pointer to the first element (in most cases).

That doesn't mean that an array is a pointer, much in the same way that when you use a function with a return type of a pointer to an int in an expression doesn't make the function itself a pointer to an int. It just simply returns that value.

Example:

int* const SomeFunction(); // Declaration of a function

...

*SomeFunction() = 2; // Sets the integer to 2



int SomeArray[10]; // Declaration of an array

...

*SomeArray = 2; // Sets the first element to 2

In those expressions, I don't know anyone who would say that SomeFunction itself is a pointer to an int, however, a lot of beginners seem to call SomeArray itself a pointer to an int. I don't really see why it's such a common misconception, but relating it to a function return type is the best why that I can try and explain how they are related. It's not exactly the same, but if you understand what's going on with the function call then you should be able to similarly understand what's going on with the use of the array name. If you want to extend the analogy with the function, using an array name "returns" a pointer to its first element. Other than that, they aren't related at all.

[edited by - Matt Calabrese on November 20, 2002 2:41:23 PM]
Advertisement
quote: Original post by smart_idiot
Pointers and arrays are virtually the same thing.


I hope I never run any of your software on my computer.
Well, my compiler can't seem to tell the difference, but maybe it's broken. Please enlighten it as to why it shouldn't be compiling this program (besides that it was written by me):


        #include <iostream>#include <cstdlib>// This does stuff to pointers.void func1(int * foo) // A function using a pointer {  ++foo;  --foo;  *foo = 1;  foo[1] = 2;  *(foo+2) = 3;  (foo+1)[2] = 4;  }// Hey, this looks exactly like func1!!!!void func2(int foo[4]) // A function using an array {  ++foo;  --foo;  *foo = 1;  foo[1] = 2;  *(foo+2) = 3;   (foo+1)[2] = 4;  }// Don't the function's signatures need to be the same to do this? Why is this working?void (*func[2])(int foo[4]) = {&func1, &func2};// This works too if you want to uncomment it and try it.// void (*func[2])(int *foo) = {&func1, &func2};// A nice unbiased way to show each type.template <class T> void ShowElements(T var) {  cout << var[0] << ' ' << var[1] << ' ' << var[2] << ' ' << var[3] << endl; }int main(void) {  // A fitting way to display the title.  char array_text[7] = "Arrays";  char *pointer_text = "Pointers";  cout << array_text << " and " << pointer_text << endl;  // An array  int foo1[4];  // A pointer (using malloc to ignore constructors and show there is nothing special happening)  int *foo2(static_cast<int *>(malloc(sizeof(int)*4)));      func[0](foo1); // using an array as a pointer.  ShowElements(foo1);    func[1](foo1); // using an array as an array.  ShowElements(foo1);    func[0](foo2); // using a pointer as a pointer.  ShowElements(foo2);    func[1](foo2); // using a pointer as an array.  ShowElements(foo2);    free(foo2); // free the data we're pointing to.    cin.get(); // Wait for the user to press a key. }    


[edited by - smart_idiot on November 20, 2002 11:58:01 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quick related question, what the heck does the ''~'' in the orginal post do? I''ve never heard of that as an operator, and as far as I was aware, it''s only used to preface destructors.
It inverts the bits in a number.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
quote: Original post by smart_idiot
Well, my compiler can''t seem to tell the difference, but maybe it''s broken. Please enlighten it as to why it shouldn''t be compiling this program (besides that it was written by me):
....(long code)...

Try replacing ''int'' with ''void'' and see if it really compiles.

"after many years of singularity, i'm still searching on the event horizon"
quote: Original post by smart_idiot
Well, my compiler can''t seem to tell the difference, but maybe it''s broken. Please enlighten it as to why it shouldn''t be compiling this program (besides that it was written by me):


You don''t know the language well enough:

void func2(int foo[4]);

Does not make a function that takes as a parameter an array of length 4. It makes a function that takes a pointer to an integer. In fact, that 4 in your declaration means absolutely nothing. The same exact declaration can be done with:

void func2(int* foo);

and

void func2(int foo[]);

Why? First, remember that arrays don''t have copy constructors, which should have been your first tip off, so obviously from the start the function declaration couldn''t be doing what you''d think it''s doing from what you wrote out. The reason that C++ provides the syntax

int foo[]

is so that the person reading the function declaration can clearly see that an array is meant to be passed via a pointer to the first element. If you just used

int* foo

It won''t be as clear from the start that you want an array to be passed.

If you REALLY wanted to test out if arrays and pointers were the same you could do make 2 different functions -- one which takes a reference to an array and one which takes a reference to a pointer. If they really were the same then a reference to an array would be the same as a reference to a pointer.

void func1(int*& foo); // Parameter is a reference to a pointer

void func2(int (&foo)[4]); // Parameter is a reference to an array of length 4

You may have thought you knew what your code was doing but you didn''t. This is a more accurate representation of what you were trying to prove:


  void func1(int*& foo); // Parameter is a reference to a pointervoid func2(int (&foo)[4]); // Parameter is a reference to an array of length 4void (*func[2])(int (&foo)[4]) = {&func1, &func2}; // Error on func1 because an array is not the same as a pointer  
quote: Original post by smart_idiot
Well, my compiler can''t seem to tell the difference, but maybe it''s broken. Please enlighten it as to why it shouldn''t be compiling this program (besides that it was written by me):


          #include <iostream>#include <cstdlib>// This does stuff to pointers.void func1(int * foo) // A function using a pointer {  ++foo;  --foo;  *foo = 1;  foo[1] = 2;  *(foo+2) = 3;  (foo+1)[2] = 4;  }// Hey, this looks exactly like func1!!!!void func2(int foo[4]) // A function using an array {  ++foo;  --foo;  *foo = 1;  foo[1] = 2;  *(foo+2) = 3;   (foo+1)[2] = 4;  }// Don''t the function''s signatures need to be the same to do this? Why is this working?void (*func[2])(int foo[4]) = {&func1, &func2};// This works too if you want to uncomment it and try it.// void (*func[2])(int *foo) = {&func1, &func2};// A nice unbiased way to show each type.template <class T> void ShowElements(T var) {  cout << var[0] << '' '' << var[1] << '' '' << var[2] << '' '' << var[3] << endl; }int main(void) {  // A fitting way to display the title.  char array_text[7] = "Arrays";  char *pointer_text = "Pointers";  cout << array_text << " and " << pointer_text << endl;  // An array  int foo1[4];  // A pointer (using malloc to ignore constructors and show there is nothing special happening)  int *foo2(static_cast<int *>(malloc(sizeof(int)*4)));      func[0](foo1); // using an array as a pointer.  ShowElements(foo1);    func[1](foo1); // using an array as an array.  ShowElements(foo1);    func[0](foo2); // using a pointer as a pointer.  ShowElements(foo2);    func[1](foo2); // using a pointer as an array.  ShowElements(foo2);    free(foo2); // free the data we''re pointing to.    cin.get(); // Wait for the user to press a key. }      


[edited by - smart_idiot on November 20, 2002 11:58:01 PM]

I don''t see the problem. I seem to recall learning that that works fine in a compsci class a long time ago.

[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
quote: Original post by ktuluorion
I don't see the problem. I seem to recall learning that that works fine in a compsci class a long time ago.


Because there isn't a problem, and the reason there isn't a problem is because the code doesn't do what smart_idiot WANTED it to do. If he did what he wanted it to do, then he would have found out that he was wrong. Refer to my previous post.

EDIT: After copying the code into my compiler (took this long cuz when i copy and paste it pastes it all as 1 line for some reason) , turns out he had some errors in his code OTHER than for what he was trying to prove so it didn't compile.

Moral of the story -- don't make a dragged out proof with mistakes in it and that isn't really a proof, especially when it can be disproven in just 3 lines

Or, if I really wanted to prove my point fast I could have done it in one:

cout << "Array: " << sizeof( char[1] ) << endl << "Pointer: " << sizeof( char* ) << endl;

If they were the same type, then their sizes would be the same.


[edited by - Matt Calabrese on November 20, 2002 12:26:14 AM]

This topic is closed to new replies.

Advertisement