DEVTOME.COM HOSTING COSTS HAVE BEGUN TO EXCEED 115$ MONTHLY. THE ADMINISTRATION IS NO LONGER ABLE TO HANDLE THE COST WITHOUT ASSISTANCE DUE TO THE RISING COST. THIS HAS BEEN OCCURRING FOR ALMOST A YEAR, BUT WE HAVE BEEN HANDLING IT FROM OUR OWN POCKETS. HOWEVER, WITH LITERALLY NO DONATIONS FOR THE PAST 2+ YEARS IT HAS DEPLETED THE BUDGET IN SHORT ORDER WITH THE INCREASE IN ACTIVITY ON THE SITE IN THE PAST 6 MONTHS. OUR CPU USAGE HAS BECOME TOO HIGH TO REMAIN ON A REASONABLE COSTING PLAN THAT WE COULD MAINTAIN. IF YOU WOULD LIKE TO SUPPORT THE DEVTOME PROJECT AND KEEP THE SITE UP/ALIVE PLEASE DONATE (EVEN IF ITS A SATOSHI) TO OUR DEVCOIN 1M4PCuMXvpWX6LHPkBEf3LJ2z1boZv4EQa OR OUR BTC WALLET 16eqEcqfw4zHUh2znvMcmRzGVwCn7CJLxR TO ALLOW US TO AFFORD THE HOSTING.

THE DEVCOIN AND DEVTOME PROJECTS ARE BOTH VERY IMPORTANT TO THE COMMUNITY. PLEASE CONTRIBUTE TO ITS FURTHER SUCCESS FOR ANOTHER 5 OR MORE YEARS!

In computer science, a pointer is a variable whose value is equal to the memory location of another variable. This can be a confusing concept, and it is useful to show some examples to solidify this definition.

A Basic Example in C

Say we have some basic integer variable “foo” and we give it an arbitrary value of 42. Then we print it to the screen.

int foo;
foo = 42;
printf("foo: %d", foo);

What has happened behind the scenes in this above block of code is the following:

  1. On creation of foo, an amount of space in memory is allocated for an integer variable called “foo”.
  2. On assignment, value “42” is assigned to be stored at the memory address belonging to variable “foo”.
  3. On printing, the value at the memory address corresponding to “foo” is retrieved and is printed to the screen. In this case, it will print “foo: 42”.

The declaration and assignment of foo could have been done in a single line with “int foo = 42;”, but they are separated for the sake of clarification in this example.

Notice that although we have assigned the value of foo, we have not yet modified or examined its memory address…although we have indirectly used it for this basic print example. But what if we wanted to store an address in a variable instead of a value?

This is where we would use a pointer, when a programmer wants to store an address rather than a value. Consider the following, where we copy the memory address of foo into a pointer variable bar.

int *bar;
bar = &foo;
*bar = 21;
printf("foo: %d", foo);

This is a bit more complicated than the first block of code, but it flows in a similar manner:

  1. On creation of bar, space in memory is assigned for an integer pointer variable called “bar”.
  2. On the assignment “bar = &foo;”, the memory address of foo is assigned to be stored at the memory address pointed at by pointer bar (specifying ”&foo” is the memory address of foo, rather than its value). This is the first confusing part.
  3. On the assignment “*bar = 21;”, the pointer bar is dereferenced via the “*” operator. This refers to the value pointed at by bar, rather than the value of bar itself. So, this line effectively writes the value “21” to the memory address pointed at by bar, which is the address of foo…so the value of foo will be set to 21.
  4. On printing, the value at the memory address corresponding to “foo” is retrieved and is printed to the screen. In this case, it will print “foo: 21” since we changed the value of foo via the pointer bar.

Why do we care about pointers?

As you can see by the above example, pointers are somewhat complicated and can be very dangerous to use; we directly access memory locations, rather than going through some sort of safe construct, so a simple off-by-one error can have disastrous results if a programmer is not careful. Despite these drawbacks, pointers can be useful for several things, including the following.

Pass-by-reference

Passing function pointers

Arrays and pointer math

Categories


QR Code
QR Code pointers (generated for current page)
 

Advertise with Anonymous Ads