Jump to content

pointers


Recommended Posts

pointer... why does that sound like a c++ thing.... oh wait are you talking about C++ pointers where you use a .... crap what is it, some character in front of the variable name and then when you use that it makes a reference point to the variable where its stored in the computers memory?I dont really think theres something like that?Why, what would you need a pointer for?

Link to post
Share on other sites

Pointers are used more in C then C++, they are used to make one variable reference a second variable's location in memory. The first variable "points" to the second one. In C, you define your pointer like this:int *ptr;And then assign it the address of another variable like this:ptr = &var;And then you can assign a value to var like this:*ptr = 10;Now var is set to 10.PHP does have variable referencing, but it's not the exact same thing as a C pointer (as the documentation immediately points out). A C pointer basically makes one variable point to the same location in memory as another variable (actually, the pointer just stores the memory address of the variable it is pointing to; accessing the value in that memory location is referred to as 'dereferencing' the pointer). A PHP reference creates an alias in the symbol table, which is basically like a list of variables and their values. You cannot reference a variable with a PHP reference in one variable scope, and still use that reference in another scope where the variable you are referencing does not exist. Since it's only a symbol table alias, and not a pointer to the same location in memory, the variable you are referencing no longer exists and in that case it won't do what you are expecting.You can read the documentation for references for more information:http://www.php.net/manual/en/language.references.php

Link to post
Share on other sites

this may be a dumb question but I have always had a hard time understanding the reason for using pointers. If you wanted to assign 10 to var why not just do it var = 10;I know that was a simple example but could you explain the reason we need pointers and what some good implementations of pointers would be?

Link to post
Share on other sites

Suppose you want a function that does something to affect a variable. Say, you have some number 10 and your code calls for transforming it to something else. Without pointers, you'd have to do this:

<?phpfunction foo ($input){ $input = $input + 1; //this simply adds 1, but insert your favorite algorithm here. return $input;}$a = 10;echo foo($a); //outputs 11echo $a; //outputs 10, notice the variable hasn't changed$a = foo($a); //function returns 11, $a is set to 11echo $a; //outputs 11?>
If you add a pointer to the function declaration - function foo (&$input), then it effectively points (via alias or whatever the heck) to the variable that was passed in.
<?phpfunction foo(&$input){ $input++; return true;}$a = 10;foo($a); //returns trueecho $a; //outputs 11, variable $a has been altered?>
I use this a lot because I do a lot of pre-processing in my script. I have separate variables for sections of the page ($text_body, $text_menu, $text_debug) and instead of using echo to put out data, I created my own function f_echo. This was so that decisions would only need to be made once and to make it simpler to handle sessions (once you output text, you can't set cookies and stuff anymore):
function f_echo(&$target, $string){ $target.=$string.'\n';}
Link to post
Share on other sites

Most of the time when I used pointers it was with a complex data structure. The C language has a 'struct', which is sort of like a class but only contains properties. So, it's just a custom data type with whatever variables you want in it. One example would be a 'node' struct:

struct node{  int value;  node *prev;  node *next;};

Say you want to declare 3 nodes that are linked into a list. It would be something like this:

node node1;node node2;node node3;node1.value = 1;node1->prev = null;node1->next = node2;node2.value = 2;node2->prev = node1;node2->next = node3;node3.value = 3;node3->prev = node2;node3->next = null;

Now, you can access the other two nodes from node2:

node2->prev.value = 10;

This type of data structure is called a doubly-linked list, since there are both previous and next pointers. You can also have just a next pointer, which makes it a singly-linked list. A doubly-linked list has several uses in C programs, it is used for things like sorting or just generally ordering data. Pointers are the most efficient way to do this because they only store the memory address, so if you have a 32-bit address space then the pointer is only 4 bytes in size, and it's also good because you can reassign the pointers to insert new nodes without affecting the actual nodes, you are just shuffling pointers around.Pointers are also the cause of a lot of subtle problems that are hard to track down. The ASU General server has been the victim of many core dumps at the hands of my pointers.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...