Jump to content
  • Advertisement

Archived

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

TwistedMatrix

so whats the deal with pointers and assignment?

This topic is 5664 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

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

Share this post


Link to post
Share on other sites
Advertisement
Those function seem to have no internal effect either
Can you be a little more specific, with what you really want?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!