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
for example, the code:

void routine(*ptr)
{
   ptr=~ptr;
   return;
}
 
would cause all kinds of weird shit to happen, however

void routine(*ptr)
{
   int a;
   a=~ptr;
   ptr=a;
   return;
}
 
works perfectly (assigns the 1''s compliment of a variable to that variable via a pointer) is this a compiler thing, or am i missing somthing? - Twisted Matrix
- Twisted Matrix
It doesn''t look like you''re dereferencing the pointer anywhere - would the functions have any external effect at all?

Also, the "return" statements are totally unnecessary.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
Those function seem to have no internal effect either
Can you be a little more specific, with what you really want?
quote: Original post by TwistedMatrix
would cause all kinds of weird shit to happen

No kidding. You haven''t given the pointer a type, so it shouldn''t get past the compiler. Weird shit, man. If you give the pointer a type, it still shouldn''t compile, since the complement operator is defined to operate on integral types, but not pointers. Care to show us the real code?
Even if it did work, you''re only screwing with your copy of the pointer, once you return nothing is going to have happened. Perhaps you want a reference to a pointer.
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.
Heres the problem. I have an array of structures. I know how to pass a pointer to a function and address it indirectly

example:
void dosomthing(struct junk *aryptr){   if (aryptr[10]->a == 21) {aryptr[43]->c=1; }   //..ect ect}int main(){   struct junk     { char a;       int b;       short int c;       unsigned char data[10];     } array[50];   dosomthing(&array[0]);   return 0;} 


This far i am right im pretty sure. could somebody give me an example of the difference between only passing a pointer to one element and passing a pointer that allows the function to acess all the elements?

you would use "struct junk **aryptr" correct?

also, how would you pass that pointer from dosomthing() to another function if you wanted to? This is giving me alot of trouble. I thought I knew pointers but I guess I was wrong.

- Twisted Matrix
- Twisted Matrix
Advertisement
Just pass the array normally. Arrays aren''t passed by value, they are always passed by reference. So just pass it like this:

void main()
{
int array[20];
SomeFunc( array );
}

void SomeFunc( int * array )
{
//do whatever you need to do with it in here.
}

and you don''t have to return the array, because it''s just pointing to the same place in memory.
is there a difference between
void SomeFunc( int * array ) and
void SomeFunc( int *array )



- Twisted Matrix
- Twisted Matrix
err..
never mind...

[edited by - TwistedMatrix on November 20, 2002 10:36:15 AM]
- Twisted Matrix
Pointers and arrays are virtually the same thing.

quote:
dosomthing(&array[0]);


This is correct, although it''s not the nicest way to do it.

Other ways include dosomthing(array+0); or just plain dosomthing(array);.

If you haven''t done addition or subtraction on a pointer, what happens is that it creates a new pointer of the same type offset by that many bytes times the size of whatever the pointer is of.

Why the heck is that useful? Well, you can do silly things like this:

  #include <iostream>void printstring(const char *string) {  while(*string != NULL) // while we aren''t pointing to a null character.   {    cout << *string; // print the character    ++string; // and move the pointer to the next character.   } }int main(void) {  printstring("Hello World");  return 0; }  


You can also use a pointer as an array with only one element, like this:


  #include <iostream>int main(void) {  int * number = new int; // create a pointer to a number  number[0] = 5; // set element 0 to 5.  cout << number[0] << endl; // display element 0.  delete number; // delete the pointer  return 0; }  


Alas, pointers and arrays are not 100% the same. (They were in C, but the introduction of classes and destructors screwed us.)

  int main(void) {  int * pointer = new int; // create a pointer  int * array = new int[1]; // create an array  delete pointer;  delete [] array;  return 0; }  


We have to do something special with the array because otherwise only the destructor for the first object in the array gets called. How does delete know how many elements are in the array? It depends on the compiler, but one common way is this:

New allocates the memory needed, plus a few bytes. The first few bytes are used to store the number of elements in the array, it calls the constructors for all the objects, and returns a pointer a few bytes ahead of the memory it allocated.

When you delete an array, delete will look behind the address you have it to find out how many elements it needs to destroy, calls their destructor, and then frees the memory.

You didn''t really need to know that, but I wanted you to know why it''s important to use the right one, because the compiler will probably let you do both.
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.

This topic is closed to new replies.

Advertisement